简体   繁体   中英

Getting NULL Deserializing JSON string using JavaScriptSerializer

Hi I having following JSON but while Using Deserialize method getting NULL in nested Member here is the sample JSON and corresponding Class object:

{
   "status": "success",
    "Info": [
      {
       "Name": "1099589",
       "version": "Current Version",
       "MoreDetails": [
        {

          "Name": "1099589",
          "state": "IN"

        },
        {
          "Name": "1099768",
          "state": "OUT"

        }

      ]
    },
    {
      "Name": "1099768",
      "version": "2019"
    }
  ],
  "errorCode": "",
  "message": ""
}

Class:

  public class MoreDetail
    {
        public string Name { get; set; }
        public string state { get; set; }
    }

    public class Info
    {
        public string Name { get; set; }
        public string version { get; set; }
        public IList<MoreDetail> MoreDetails { get; set; }
    }

    public class Example
    {
        public string status { get; set; }
        public IList<Info> Info { get; set; }
        public string errorCode { get; set; }
        public string message { get; set; }
    }

While I am using

JavaScriptSerializer js = new JavaScriptSerializer();
Example ex = new OfferingPayload();
ex = js.Deserialize<Example> (jsonstring);

I am able to see Example object having Info data as list but MoreDetails member of Info Class is coming NULL.

Can someone suggest what I am missing here?

Thats because your second "Info" object doesnt have "MoreDetails" property.

{
  "Name": "1099768",
  "version": "2019"
}

To make it works you can add an empty "MoreDetails" property to your json.

{
  "Name": "1099768",
  "version": "2019",
  "MoreDetails": []
}

Or you can configure your serializer to handle this property as optional. Then it will works fine even if it missing in your json.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM