Using JSON in C# deserialized into a dictionary -


i deserialized json data dictionary. json data looks like:

{     "inputkoffretstats":true,"inputpenaltystats":false,     "visitingteamfootballrbstats":          [{              "athletelastname":"john",              "athletefirstname":"smith","number":9,"ispresent":true          },          {              "athletelastname":"justin",              "athletefirstname":"brooks","number":10,"ispresent":false          }] } 

and how deserialized string dictionary:

var dict = jsonconvert.deserializeobject<dictionary<string, object>>(result); 

the problem have not know how get, example, first athlete's last name (john).

if this:

 console.writeline(dict["inputkoffretstats"]); 

then "true". works fine.

if this:

console.writeline(dict["visitingteamfootballrbstats"]); 

then information of both athletes (john , justin).

how use dictionary first athletes name? tried doing following no success:

console.writeline(dict["visitingteamfootballrbstats"][0]); 

any ideas?

edit: should have mentionned json showed small portion of whole json. gave example.

a full working example:

public class athlete {     public string athletelastname { get; set; }     public string athletefirstname { get; set; }     public int number { get; set; }     public bool ispresent { get; set; } }  public class stat {     public bool inputkoffretstats { get; set; }     public bool inputpenaltystats { get; set; }     public list<athlete> visitingteamfootballrbstats { get; set; } }  class program {     static void main(string[] args)     {         var jsontext = "{" +                         "    \"inputkoffretstats\":true,\"inputpenaltystats\":false," +                         "    \"visitingteamfootballrbstats\":" +                         "         [{" +                         "             \"athletelastname\":\"john\"," +                         "             \"athletefirstname\":\"smith\",\"number\":9,\"ispresent\":true" +                         "         }," +                         "         {" +                         "             \"athletelastname\":\"justin\"," +                         "             \"athletefirstname\":\"brooks\",\"number\":10,\"ispresent\":false" +                         "         }]" +                         "}";          var stat = jsonconvert.deserializeobject<stat>(jsontext);          console.writeline(stat.visitingteamfootballrbstats[0].athletefirstname);         console.readkey();     } } 

ps.: there error in json provided. once you're using "ispresent" boolean: "ispresent":true, next time you're using string: "ispresent":no (incorrectly, because no should "no"). anyway, corrected "ispresent":false.

update: if want "javascript-like" behavior, want able someobj["someproperty"]["somenestedproperty"] can this:

var stat = jsonconvert.deserializeobject<jobject>(jsontext); 

so first name of first athlete:

var firstname = stat["visitingteamfootballrbstats"][0]["athletefirstname"].value<string>(); 

that said, still create proper classes possible fields in json.


Comments