1

I have this JSON object which I want to deserialize

{
    "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."
}
1
  • Paste your JSON into QuickType.io - it will generate C# for you that Just Works. Other similar services exist (json2csharp for example) that do a similar thing. Generating c# from json is one of those boring boilerplate jobs that we shouldn't even be paining the most junior apprentice with 😀; devolve it to a computer. By the way if this json is coming from an api that publishes an openapi/swagger, feed the swagger spec into nswag/autorest for even more autonomization Commented Jan 8, 2022 at 10:03

1 Answer 1

2

I think the problem is with your Body class, the correct class will be like this.

because the body node does not contain an array but a single 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

Sign up to request clarification or add additional context in comments.

1 Comment

I would suggest a Visual Studio built-in feature named "Paste JSON as classes" instead third party tools. It is faster.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.