【问题标题】:Unable to deserialize JSON in c#无法在 C# 中反序列化 JSON
【发布时间】:2017-09-29 08:02:25
【问题描述】:

我从 REST API 获得以下 JSON 响应。

{
   "data":{
      "id":123,
      "zoneid":"mydomain.com",
      "parent_id":null,
      "name":"jaz",
      "content":"172.1 6.15.235",
      "ttl":60,
      "priority":null,
      "type":"A",
      "regions":[
         "global"
      ],
      "system_record":false,
      "created_at":"2017-09-28T12:12:17Z",
      "updated_at":"2017-09-28T12:12:17Z"
   }
}

并尝试使用以下代码进行解析,但这不会导致正确的反序列化类型。

var model = JsonConvert.DeserializeObject<ResponseModel>(response);           

下面是根据我在 JSON 响应中收到的字段的类。

 public class ResponseModel
{
    public int id { get; set; }
    public string zone_id { get; set; }
    public int parent_id { get; set; }
    public string name { get; set; }
    public string content { get; set; }
    public int ttl { get; set; }
    public int priority { get; set; }
    public string type { get; set; }
    public string[] regions { get; set; }
    public bool system_record { get; set; }
    public DateTime created_at { get; set; }
    public DateTime updated_at { get; set; }

}

缺少什么?

【问题讨论】:

    标签: c# json rest api json-deserialization


    【解决方案1】:

    你缺少一个包装类。

    public class Wrapper 
    {
       public ResponseModel data {get;set}
    }
    

    然后做:

    var model = JsonConvert.DeserializeObject<Wrapper>(response).data; 
    

    data 属性中获取您的ResponseModel 实例。

    你可以从你的 json 中扣除这个:

    { "data": 
       { "id":123, /*rest omitted */ }
    }
    

    将接收此 JSON 的类型需要有一个名为 data 的属性。建议的 Wrapper 类充当该类型。

    【讨论】:

      【解决方案2】:

      根据 json2csharp 网站,您的模型似乎不正确。试试这个:

      public class ResponseModel
      {
          public int id { get; set; }
          public string zoneid { get; set; }
          public object parent_id { get; set; }
          public string name { get; set; }
          public string content { get; set; }
          public int ttl { get; set; }
          public object priority { get; set; }
          public string type { get; set; }
          public List<string> regions { get; set; }
          public bool system_record { get; set; }
          public DateTime created_at { get; set; }
          public DateTime updated_at { get; set; }
      }
      
      public class RootObject
      {
          public ResponseModel data { get; set; }
      }
      

      【讨论】:

        【解决方案3】:

        这是您可以在 Visual Studio 2015-2017 中执行的一个很酷的技巧,如果您只需复制 JSON (ctrl + c),它就会生成正确的类。

        您需要在 Visual Studio 中创建一个新类,进入该类后转到编辑菜单 -> 选择性粘贴 -> 将 JSON 粘贴为类。

        Steps to generate json class

        这将为您生成该 json 的 C# 对象并为您省去所有麻烦:)

        【讨论】:

        • 非常好的技巧。与 json2csharp 相同,但在 IDE 中。谢谢
        【解决方案4】:

        您的模型与您的响应不匹配 - 它与 data 属性匹配。只需将另一个对象包裹在它周围

        public class ResponseData
        {
            public ResponseModel Data {get; set; {
        }
        

        然后

        var model = JsonConvert.DeserializeObject<ResponseData>(response); 
        

        【讨论】:

          猜你喜欢
          • 2018-10-18
          • 1970-01-01
          • 1970-01-01
          • 2021-09-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多