无法将json对象反序列化为模型类类型

时间:2020-09-19 07:17:33

标签: c# json

我有下面的JSON数据和Model类。我尝试将JSON对象反序列化为Model类数据,但显示错误消息:< / p>

无法将JSON对象反序列化为System.Collections.Generic.List`1 [eInvoice.Model.errors]'。

我尝试了下面的代码,但是它不起作用。
请检查此代码,并告诉我如何将JSON对象反序列化为Model类数据。

Json对象:

 [
  {
    "custom_fields": null,
    "document_status": "NOT_CREATED",
    "error_response": null,
    "errors": {
      "errors": [
        {
          "error_code": "101",
          "error_message": "Invoice with the Same Transaction ID is Present In the Database",
          "error_source": "CLEARTAX"
        }
      ]
    },
    "govt_response": {
      "Success": "N",
      "ErrorDetails": [
        {
          "error_code": "2150",
          "error_message": "Duplicate IRN",
          "error_source": "GOVT"
        }
      ],
      "info": [
        {
          "InfCd": "DUPIRN",
          "Desc": {
            "AckNo": 112010005972781,
            "AckDt": "2020-09-19 10:17:00",
            "Irn": "7ed6484c750e09c2bbaea84ce69909b434e0a9e8767f0e535e04eb268f3a5a1b"
          }
        }
      ]
    },
    "group_id": null,
    "gstin": "29AAFCD5862R000",
    "owner_id": "78c6beda-54a2-11ea-b064-0af3f8b02c24",
    "transaction": {
     },
    "transaction_id": "29AAFCD5862R000_G/E/20-21/0187_INV_2020",
    "transaction_metadata": null
  }
]

模型类别:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace eInvoice.Model
{
    public class responses
    {        
        public response response { get; set; }

    }
    public class response
    {
        public string custom_fields { get; set; }
        public string document_status { get; set; }
        public string error_response { get; set; }
        public List<errors> errors { get; set; }
        public govt_response govt_response { get; set; }
        public string group_id { get; set; }
        public string gstin { get; set; }
        public string owner_id { get; set; }

        public transaction transaction { get; set; }

    }

    public class errors
    {
        public string error_code { get; set; }
        public string error_message { get; set; }
        public string error_source { get; set; }
        
    }
    public class govt_response
    {
        public string Success { get; set; }
        public string AckNo { get; set; }
        public string AckDt { get; set; }
        public string Irn { get; set; }
        public string SignedInvoice { get; set; }
        public string SignedQRCode { get; set; }
        public string Status { get; set; }
        public List<ErrorDetails> ErrorDetails { get; set; }
        public List<info> info { get; set; }
    }
    public class ErrorDetails
    {
        public string error_code { get; set; }
        public string error_message { get; set; }
        public string error_source { get; set; }

    }

    public class info
    {
        public string InfCd { get; set; }
    }
    public class Desc
    {
        public string AckNo { get; set; }
        public string AckDt { get; set; }
        public string Irn { get; set; }

    }

}

反序列化的源代码:

List<response> res = JsonConvert.DeserializeObject<List<response>>(jsonstring);

1 个答案:

答案 0 :(得分:0)

在您的json文件中,您首先拥有一个名为errors的json对象,该对象具有数组errors。但是您的response类包含类型List<errors>的属性,因此我认为它应该具有定义类型List<errors>的属性的类型的属性。

将您的errors类更改为类似的内容,并添加一个新类以表示单个错误。我认为应该可以。

public class jsonErrors
{
   public List<error> errors { get; set; }
}

public class error
{
   public string error_code { get; set; }
   public string error_message { get; set; }
   public string error_source { get; set; }
}

另外,您还应该将响应类中的errors属性更改为:

public class response
{
    ...
    public jsonErrors errors { get; set; }
    ...
}

我还要提醒您注意如何命名类和属性。通常,C#中的类和属性使用首字母大写的 CamelCase 样式命名。