Iterating through JSON using System.Json

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 19:03:34

问题


I'm exploring the capabilities of .NET 4.5 System.Json library but there isn't much documentation, and it's quite tricky to search for due to the popular JSON.NET library.

I am wondering basically, how I'd loop through some JSON for example:

{ "People": { "Simon" : { Age: 25 }, "Steve" : { Age: 15 } } }

I have my JSON in a string, and I want to iterate through and display the ages of everyone.

So first I'd do:

var jsonObject = JsonObject.Parse(myString);

but then I'm at a loss of what to do next. I'm surprised that the parse method returns a JsonValue not a JsonObject.

What I want to do really is:

foreach (var child in jsonObject.Children)
{
  if (child.name == "People")
{
 // another foreach to loop over the people
 // get their name and age, eg. person.Name and person.Children.Age (LINQ this or something)

}

}

any ideas?


回答1:


Since the accepted answer does not helped me much because its one of the typcial useles "use this lib its better!" answers i figured that out.

Yes,

JsonObject.Parse(myJsonString);

Returns a JsonValue object that is the base class of all the Json* classes from System.Json. When you call JsonObject.Parse(..) then JsonValue.Parse() is really being called because of the inheritance.

to iterate over a JsonObject you can use:

var jsonObject = JsonValue.parse(jsonString);

foreach (KeyValuePair<string, JsonValue> value in jsonObject)
{
    Console.WriteLine("key:" + value.Key);
    Console.WriteLine("value:" + value.Value);
}

I dont tried out if this also works if its an JsonArray but maybe if its an JsonArray then you might want to do it with a classic for i:

for (var i = 0; i < jsonObject.Count; i++)
{
    Console.WriteLine("value:" + jsonObject[i]);
}

If you dont know if its an array or an object than you might check that before

if (jsonObject.GetType() == typeof JsonObject){
    //Iterate like an object 
}else if (jsonObject.GetType() == typeof JsonArray){
    //iterate like an array
}



回答2:


Using Json.Net and some Linq

string json = @"{ ""People"": { ""Simon"" : { Age: 25 }, ""Steve"" : { Age: 15 } } }";

var people =  JsonConvert.DeserializeObject<JObject>(json)["People"];

var dict = people.Children()
                 .Cast<JProperty>()
                 .ToDictionary(p => p.Name, p => p.Value["Age"]);



回答3:


for that kindly use json.net library its much better then the system.json itself

urclassobj = await JsonConvert.DeserializeObjectAsync<urclass>(json string)

then using foreach with linq on your list of objects

 foreach(var details in urclassobj
                        .Select((id) => new { id= id})
                        )
 {
   Console.WriteLine("{0}", details.id);
  } 

and for object to json its

 string json2Send = await JsonConvert.SerializeObjectAsync(urclassobject);


来源:https://stackoverflow.com/questions/16338917/iterating-through-json-using-system-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!