c# - Parsing JSONObject -


i trying json webservice , able figure out . btw json data -

   {     "x-yz-111/ab.cd": {         "p1": "f",         "p2": "43.46"     },              "x-yz-112/ab.cd": {         "p1": "f",         "p2": "8.02"     },     "x-yz-113/ab.cd": {         "p1": "f",         "p2": "9066.58"     },     "x-yz-114/ab.cd": {         "p1": "f",         "p2": "6.00"     },     "x-yz-115/ab.cd": {         "p1": "f",         "p2": "6.00"     },             "x-yz-116/ab.cd": {         "p1": "f",         "p2": "10.00"     }}   using windows.data.json;  private async void getjson_click(object sender,routedeventargs e)  {     var client=new httpclient();    client.maxresponsebuffersize=1024*1024;    var response= await client.getasync(new uri(the url here));    var result = await response.content.readasstringasync();     var component=jsonobject.parse(result);  } 

now able trying parse collection,where each item has "x-yz-111/ab.cd" name property , p1 , p2 other 2 properties ,which try bind tile in ui using collectionviewsource later.

now c# classes json2cshardpdotcom generating

public class name {     public string type { get; set; }     public string val { get; set; } }  public class root  {     public name name { get; set; } } 

any suggestions on how loop through these values using foreach , classes should make in c#?

edit 1: btw know there has dictionary first foreach on outer loop retrieve name , foreach on inner loop retrieve p1 , p2.

but confused regarding classes should use in c# consume json , create collection item out of json

i confused regarding classes should use in c# consume json , create collection item out of json

you don't need class. little bit linq

i use json.net 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 all. sample usage 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"])); 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -