【问题标题】:How to deserialize this JSON with JsonConvert.DeserializeObject如何使用 JsonConvert.DeserializeObject 反序列化此 JSON
【发布时间】:2016-03-21 15:05:45
【问题描述】:

我正在尝试反序列化此 JSON,但我不断收到错误消息。有人可以帮帮我吗?我哪里做错了?

JSON:

{
    "totalItems": 63,
    "items": [
    {
        "id": 100039812,
        "group": {
            "code": "DD",
            "description": "Delivery Documents"
        },
        "type": {
            "code": "READ",
            "description": "Logs"
        },
        "reference": "ARLDR",
        "date": "2015-03-24T00:00:00",
        "description": "ALogs",
        "notes": "",
        "lastUpdateDate": "2015-03-24T14:06:42.063",
        "location": "BOX A001",
        "metadata": {}
    },
    {
        "id": 100039813,
        "group": {
            "code": "DD",
            "description": "Delivery Documents"
        },
        "type": {
            "code": "BL",
            "description": "Logbooks"
        },
        "reference": "BALB",
        "date": "2015-03-24T00:00:00",
        "description": "Logbooks",
        "notes": "",
        "lastUpdateDate": "2015-03-24T14:07:42.44",
        "location": "BOX A001",
        "metadata": {}
        }
    ]
}

public class Documents
{
    public int totalItems  { get; set; }
    public List<doc_items> items { get; set; }
}

public class doc_items
{
    public int id { get; set; }
    public List<group_items> group { get; set; }
    public List<type_items> type { get; set; }
    public string reference { get; set; }
    public string date { get; set; }
    public string description { get; set; }
    public string notes { get; set; }
    public string lastUpdateDate { get; set; }
    public string location { get; set; }
    public List<metadata_list> metadata { get; set; }
}

public class group_items
{
    public string code { get; set; }
    public string description { get; set; }
}

public class type_items
{
    public string code { get; set; }
    public string description { get; set; }
}

public class metadata_list
{

}

然后我称之为:

Documents myDocuments = JsonConvert.DeserializeObject<Documents>(responsetext.ToString());

并收到以下错误:

错误:Newtonsoft.Json.JsonSerializationException: 无法将当前 JSON 对象(例如 {"name":"value"})反序列化为 System.Collections.Generic.List`1[AerData.Ranorex.Web.Records.API_Documents+Documents]' 类型,因为该类型需要 JSON 数组(例如 [...

【问题讨论】:

  • 使用编辑 -> 选择性粘贴 -> 将 JSON 粘贴为类,或使用 jsonutils.com 将您的 json 字符串粘贴到新的类文件中 - 您的类不完全匹配

标签: c# json serialization deserialization


【解决方案1】:

相反,“组”、“类型”和“元数据”不是数组,因此请将您的类修改为:

public class doc_items
{
    public int id { get; set; }
    public group_items group { get; set; }
    public type_items type { get; set; }
    public string reference { get; set; }
    public string date { get; set; }
    public string description { get; set; }
    public string notes { get; set; }
    public string lastUpdateDate { get; set; }
    public string location { get; set; }
    public metadata_list metadata { get; set; }
}

【讨论】:

    【解决方案2】:

    根据Documents 类的定义,doc_itemsgroup 属性必须是一个数组。在您给定的 JSON 中,它是一个对象。 type 属性也是如此

    【讨论】:

      【解决方案3】:

      我建议使用这样的转换器:http://json2csharp.com/#

      试试这个:

      class Program
      {
          static void Main(string[] args)
          {
              var sr = new StreamReader("json.json");
              var jsonText = sr.ReadToEnd();
              Documents myDocuments = JsonConvert.DeserializeObject<Documents>(jsonText);
          }
      }
      
      public class group_items
      {
          public string code { get; set; }
          public string description { get; set; }
      }
      
      public class type_items
      {
          public string code { get; set; }
          public string description { get; set; }
      }
      
      public class metadata_list
      {
      }
      
      public class doc_items
      {
          public int id { get; set; }
          public group_items GroupItems { get; set; }
          public type_items TypeItems { get; set; }
          public string reference { get; set; }
          public string date { get; set; }
          public string description { get; set; }
          public string notes { get; set; }
          public string lastUpdateDate { get; set; }
          public string location { get; set; }
          public metadata_list MetadataList { get; set; }
      }
      
      public class Documents
      {
          public int totalItems { get; set; }
          public List<doc_items> items { get; set; }
      }
      

      您的数据文件json.json

      {
        "totalItems": 63,
        "items": [
          {
            "id": 100039812,
            "group": {
              "code": "DD",
              "description": "Delivery Documents"
            },
            "type": {
              "code": "READ",
              "description": "Logs"
            },
            "reference": "ARLDR",
            "date": "2015-03-24T00:00:00",
            "description": "ALogs",
        "notes": "",
        "lastUpdateDate": "2015-03-24T14:06:42.063",
        "location": "BOX A001",
        "metadata": { }
      },
      {
        "id": 100039813,
        "group": {
          "code": "DD",
          "description": "Delivery Documents"
        },
        "type": {
          "code": "BL",
          "description": "Logbooks"
        },
        "reference": "BALB",
        "date": "2015-03-24T00:00:00",
        "description": "Logbooks",
        "notes": "",
        "lastUpdateDate": "2015-03-24T14:07:42.44",
        "location": "BOX A001",
        "metadata": { }
          }
        ]
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-29
        • 2012-06-22
        • 2020-12-04
        • 1970-01-01
        • 2022-12-20
        • 1970-01-01
        相关资源
        最近更新 更多