简体   繁体   English

无法反序列化当前 JSON object C#

[英]Cannot deserialize the current JSON object C#

I have this JSON object which I want to deserialize我有这个 JSON object 我想反序列化

{
    "LoanInformationInqRs": {
        "MsgRsHdr": {
            "ResponseStatus": {
                "StatusDesc": "Success",
                "StatusCode": "I000000"
            }
        },
        "Body": {
            "LoanInformationList": {
                "LoanInformation": [
                    {
                        "LoanAmount": "0",
                        "PaidAmount": "0",
                        "RemainingAmount": "0",
                        "TotalDueAmount": "0"
                    }
                ]
            }
        }
    }
}

I have tried我努力了

public class LoanInfoDto
    {
        public LoanInformationInqRs LoanInformationInqRs { get; set; }
    }

public class LoanInformationInqRs
    {
        public MsgRsHdr MsgRsHdr { get; set; }
        public Body Body { get; set; }
    }

public class Body
    {
        public List<LoanInfo> LoanInformationList { get; set; }
    }

public class LoanInfo
    {
       
        public string TotalLateInstallments { get; set; }
        public string PaidAmount { get; set; }
        public string RemainingAmount { get; set; }
        public string TotalDueAmount { get; set; }


    }

    string res = httpResponse.Content.ReadAsStringAsync().Result;
    LoanInfoDto response = JsonConvert.DeserializeObject<LoanInfoDto>(res);

I got this error我收到了这个错误

{
  "Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.List`1[Entities.LoanInfo]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\r\nPath 'LoanInformationInqRs.Body.LoanInformationList.LoanInformation', line 1, position 152."
}

I think the problem is with your Body class, the correct class will be like this.我认为问题出在您的Body class 上,正确的 class 将是这样的。

because the body node does not contain an array but a single object.因为body节点不包含数组,而是单个 object。

public class ResponseStatus
{
    public string StatusDesc { get; set; }
    public string StatusCode { get; set; }
}

public class MsgRsHdr
{
    public ResponseStatus ResponseStatus { get; set; }
}

public class LoanInformation
{
    public string LoanAmount { get; set; }
    public string PaidAmount { get; set; }
    public string RemainingAmount { get; set; }
    public string TotalDueAmount { get; set; }
}

public class LoanInformationList
{
    public List<LoanInformation> LoanInformation { get; set; }
}

public class Body
{
    public LoanInformationList LoanInformationList { get; set; }
}

public class LoanInformationInqRs
{
    public MsgRsHdr MsgRsHdr { get; set; }
    public Body Body { get; set; }
}

public class LoanInfoDto
{
    public LoanInformationInqRs LoanInformationInqRs { get; set; }
}

I would suggest you use json2csharp converting tool to help you to get those classes我建议您使用json2csharp转换工具来帮助您获得这些类

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

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