How to Deserialize a JSON array in C# -


i struggling subject has lot of variants in forum can't seem find 1 suits me, , think it's because of way json array :( i'm not expert manage "almost" end.. need hand in "status" , "listofcreddetails" value.

my json (is called responsefromserver):

 {  "status": {              "statuscode":143,              "substatus":0,              "description":"ok"            },  "listofcreddetails":                   [{                      "client":"a",                      "credid":111,                      "creduserid":"abc"                    },                    {                      "client":"b",                      "credid":112,                      "creduserid":"def"                    },                    {                      "client":"c",                      "credid":113,                      "creduserid":"ghi"                    }]   } 

then, based on lot of examples in forum, taking bits , pieces created classes:

 [serializable]  public class statusreturn     {         public int statuscode { get; set; }         public int substatus { get; set; }         public string description { get; set; }     }    [serializable] public class creddetailsreturn {     public string client{ get; set; }     public int credid{ get; set; }     public string creduserid{ get; set; }  }   [serializable] public class getusercredentialdetailsreturn {     public statusreturn status;     public list<creddetailsreturn> listofcreddetails;      public getusercredentialdetailsreturn()     {         status = new statusreturn();         listofcreddetails = new list<creddetailsreturn>();      } } 

then going deserialize

1."status" , elements 1 object and

2."listofcreddetails" , list of elements 1 object

and creating object "getusercredentialdetailsreturn" return both status(object) , listofcreddetails(object) @ time.

can me understand how can achieve have tried below deserialize , json data 2 seperate objects.

but not working....

public getusercredentialdetailsreturn invokerequest(restinvokeclass objinvoke)     {        ...       ...          using (var streamreader = new streamreader(httpresponse.getresponsestream()))         {              string responsetext = streamreader.readtoend();              getusercredentialdetailsreturn result = new getusercredentialdetailsreturn();             result.status = jsonconvert.deserializeobject<statusreturn>(responsetext);             result.listofcreddetails  = jsonconvert.deserializeobject<list<creddetailsreturn>>(responsetext);              return result;           }      } 

you need first change property names in class getusercredentialdetailsreturnas status status , listofcreddetails listofcreddetails.

then can try de-serialize json class getusercredentialdetailsreturnby code below.

getusercredentialdetailsreturn result = new getusercredentialdetailsreturn(); result = jsonconvert.deserializeobject<getusercredentialdetailsreturn>(responsetext); 

you can status , listofcreddetails in result.

   // result.status    // result.listofcreddetails 

hope helpful you.


Comments