c# - Iterating through JSON using System.Json -


i'm exploring capabilities of .net 4.5 system.json library there isn't documentation, , it's quite tricky search due popular json.net library.

i wondering basically, how i'd loop through json example:

{ "people": { "simon" : { age: 25 }, "steve" : { age: 15 } } }

i have json in string, , want iterate through , display ages of everyone.

so first i'd do:

var jsonobject = jsonobject.parse(mystring);

but i'm @ loss of next. i'm surprised parse method returns jsonvalue not jsonobject.

what want is:

foreach (var child in jsonobject.children) {   if (child.name == "people") {  // foreach loop on people  // name , age, eg. person.name , person.children.age (linq or something)  }  } 

any ideas?

using json.net , 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"]); 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -