Reputation: 15
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."
}
Upvotes: 1
Views: 613
Reputation: 46219
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
Upvotes: 2