Parsing JSONObject

此生再无相见时 提交于 2019-12-08 07:13:28

i am confused regarding what classes i should use in C# to consume JSON and create collection item out of my JSON

You don't need any class. Just a little bit Linq

I would use Json.Net for this

var jObj = JObject.Parse(result);
var dict = jObj.Children()
           .Cast<JProperty>()
           .ToDictionary(p => p.Name, 
                         p => new Tuple<string, string>((string)p.Value["P1"], (string)p.Value["P2"]));

That is all. Sample usage would be:


foreach (var kv in dict)
{
    Console.WriteLine("{0}: {1} {2}", kv.Key, kv.Value.Item1, kv.Value.Item2);
}

EDIT

Same code using JsonObject

var jObj = JsonObject.Parse(json);
var dict = jObj.Cast<KeyValuePair<string, JsonValue>>()
           .ToDictionary(j=>j.Key,
                         j=>new Tuple<string,string>(j.Value["P1"],j.Value["P2"]));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!