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
Post a Comment