c# - Deserialize Json Web API Response - Nested objects -


i getting web api json response via ssis script component, coming in following format:

    {   "success": true,   "response": {     "applications": [       {         "app_id": 1638486,         "app_ref": "test example",         "status": "complete",         "error_code": null,         "error_message": null,         "create_dt": "2014-05-14 03:09:01.030 +00:00",         "modify_dt": "2014-05-14 03:10:59.757 +00:00",         "client_name": "silver chef",         "client_code": "slvc01",         "centrelink_status": "receiving_logons"       },       {         "app_id": 1637906,         "app_ref": "sme demo",         "status": "complete",         "error_code": null,         "error_message": null,         "create_dt": "2015-10-08 03:07:26.793 +00:00",         "modify_dt": "2015-10-08 03:23:32.833 +00:00",         "client_name": "silver chef",         "client_code": "slvc01",         "centrelink_status": "receiving_logons"       },       {         "app_id": 1585286,         "app_ref": "test",         "status": "receiving_logons",         "error_code": null,         "error_message": null,         "create_dt": "2015-12-04 03:12:49.617 +00:00",         "modify_dt": "2015-12-04 03:12:49.617 +00:00",         "client_name": "silver chef",         "client_code": "slvc01",         "centrelink_status": "receiving_logons"       } ],     "current_dt": "2016-11-11 01:01:01.743 +00:00",     "last_app_dt": "2016-10-03 22:48:56.397 +00:00",     "count": 500,     "total": 1870   } } 

i have declared following classes:

     public class application         {             public int app_id { get; set; }             public string app_ref { get; set; }             public string status { get; set; }             public int error_code { get; set; }             public string error_message { get; set; }             public datetime create_dt { get; set; }             public datetime modify_dt { get; set; }             public string client_name { get; set; }             public string client_code { get; set; }             public int centrelink_status { get; set; }          }         public class response         {             public list<application> applications { get; set; }             public string current_dt { get; set; }             public string last_app_dt { get; set; }             public int count { get; set; }             public int total { get; set; }         }          public class rootobject         {             public bool success { get; set; }             public response response { get; set; }         }  , method using response.  private rootobject getwebserviceresult(string vapiurl)     {          string vapitoken = variables.apitoken;          //create web request         httpwebrequest apireq = (httpwebrequest)webrequest.create(vapiurl);         apireq.contenttype = "application/json";         apireq.method = "post";          string jsonpoststr = "{\"settings\": {\"api_token\": \"" + vapitoken + "\"}, \"payload\": {}}";         byte[] poststring = encoding.utf8.getbytes(jsonpoststr);          apireq.contentlength = poststring.length;          stream jsonstream = apireq.getrequeststream();          jsonstream.write(poststring, 0, poststring.length);         jsonstream.close();          // web response                 httpwebresponse apirsp = (httpwebresponse)apireq.getresponse();         rootobject jsonresponse = null;          stream jsonrspstream = apirsp.getresponsestream();         string apiresponsestring = null;          using (streamreader reader = new streamreader(jsonrspstream))          {             apiresponsestring = reader.readtoend();              console.writeline(apiresponsestring);             reader.close();         }           javascriptserializer returnjson = new javascriptserializer();          //var serialjsonstr = returnjson.serialize(apiresponsestring);         system.windows.forms.messagebox.show(apiresponsestring);          jsonresponse = returnjson.deserialize<rootobject>(apiresponsestring);          return jsonresponse;      } 

my issue returnjson.deserialize(apiresponsestring); seems return null , subsequently makes fail.

what missing? think code blind @ stage.

thanks in advance

there couple of problems application class:

  • you have defined application.error_code integer:

    public int error_code { get; set; } 

    but in fact, in json, has null value:

    "error_code": null, 

    thus error_code needs reference type (string) or nullable value type (int?) depending on holds when non-null.

  • you have defined centrelink_status int, in json it's string:

    "centrelink_status": "receiving_logons" 

    thus needs string in data model well.

thus application class should like:

public class application {     public int app_id { get; set; }     public string app_ref { get; set; }     public string status { get; set; }     public int? error_code { get; set; } // or string, if not numeric     public string error_message { get; set; }     public datetime create_dt { get; set; }     public datetime modify_dt { get; set; }     public string client_name { get; set; }     public string client_code { get; set; }     public string centrelink_status { get; set; } } 

you might want make datetime properties nullable if there chance null value in json.

incidentally, when tried deserialize json, got exception thrown, rather null value returned:

system.invalidoperationexception unhandled   message="cannot convert null value type."   source="system.web.extensions"   stacktrace: 

in actual code, catching , swallowing exceptions? that's bad idea precisely because tends hide errors such this. @ minimum, when debugging should break whenever exception thrown including first-chance exception - though not default behavior. see managing exceptions debugger. if find there types of exception thrown , irrelevant debugging, can selectively re-ignore them. had done bug have been more straightforward track down. admittedly, error not explanatory, give hint. if use deserialize json, error message clearer:

newtonsoft.json.jsonserializationexception occurred   message="error converting value {null} type 'system.int32'. path 'response.applications[0].error_code', line 9, position 26."   source="newtonsoft.json"   stacktrace:        @ newtonsoft.json.serialization.jsonserializerinternalreader.ensuretype(jsonreader reader, object value, cultureinfo culture, jsoncontract contract, type targettype) in c:\development\releases\json\working\newtonsoft.json\working-signed\src\newtonsoft.json\serialization\jsonserializerinternalreader.cs:line 986   innerexception: system.invalidcastexception        message="null object cannot converted value type."        source="mscorlib"        stacktrace:             @ system.convert.changetype(object value, type conversiontype, iformatprovider provider)             @ newtonsoft.json.serialization.jsonserializerinternalreader.ensuretype(jsonreader reader, object value, cultureinfo culture, jsoncontract contract, type targettype) in c:\development\releases\json\working\newtonsoft.json\working-signed\src\newtonsoft.json\serialization\jsonserializerinternalreader.cs:line 979        innerexception:  

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -