简体   繁体   English

使用 JavaScriptSerializer 获取 NULL 反序列化 JSON 字符串

[英]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: 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: 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.我可以看到示例 object 将 Info 数据作为列表,但 Info Class 的 MoreDetails 成员即将推出 NULL。

Can someone suggest what I am missing here?有人可以建议我在这里缺少什么吗?

Thats because your second "Info" object doesnt have "MoreDetails" property.那是因为您的第二个“信息”object 没有“更多详细信息”属性。

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

To make it works you can add an empty "MoreDetails" property to your json.要使其正常工作,您可以向 json 添加一个空的“MoreDetails”属性。

{
  "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.即使 json 中缺少它,它也会正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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