Cannot deserialize the current JSON object into type 'System.Collections.Generic.List

邮差的信 提交于 2020-06-23 12:20:31

问题


I am trying to read my JSON result.

Here is my JsonResult

public class JsonResult
    {
    public string ResponseStatus;
    public string Status;
    public string Remarks;
    public string ErrorCode;
    public List<Data> data;    


    }
public class Data
{
    public string Status;
    public DateTime Date;
    public string Number;
    public string Amount;
    public string Balance;
    public string TranId;
    public string OPTId;
    public string RefId;

}

Here is my JSON result

{
"ResponseStatus":"1",
"Status":"Success",
"Remarks":"Your recharge has been Pending.",
"ErrorCode":"3",
"Data":
    {
    "Status":"Pending",
    "Date":"2017-02-23T17:22:26.2001954+05:30",
    "Number":"9915933511",
    "Amount":10.0,
    "Balance":137.714,
    "TranId":"1126887",
    "OPTId":"","RefId":""
    }
}

Here is my code to read the result

var result = JsonConvert.DeserializeObject<List<JsonResult>>(retPKT);

But Getting Exception..

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[JsonResult+Data]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To 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) 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. Path 'Data.Status', line 1, position 118.

How can I fix this?


回答1:


In your Json string, Data is an object, not an array. So your JsonResult class should look like this:

public class JsonResult
{
    public string ResponseStatus;
    public string Status;
    public string Remarks;
    public string ErrorCode;
    public Data Data;    
}



回答2:


If you want to send list of data List<Data> then in JSON it should be in a form of array

Change the following JSON content

"Data": {"Status":"Pending",
"Date":"2017-02-23T17:22:26.2001954+05:30",
"Number":"9915933511",
"Amount":10.0,"Balance":137.714,
"TranId":"1126887","OPTId":"","RefId":""}

To

"Data": [{"Status":"Pending",
"Date":"2017-02-23T17:22:26.2001954+05:30",
"Number":"9915933511",
"Amount":10.0,"Balance":137.714,
"TranId":"1126887","OPTId":"","RefId":""}]



回答3:


Data isn't a list, it's an object. Your models should look like this (Only JsonResult is modified):

public class Data
{
    public string Status { get; set; }
    public string Date { get; set; }
    public string Number { get; set; }
    public double Amount { get; set; }
    public double Balance { get; set; }
    public string TranId { get; set; }
    public string OPTId { get; set; }
    public string RefId { get; set; }
}

public class JsonResult
{
    public string ResponseStatus { get; set; }
    public string Status { get; set; }
    public string Remarks { get; set; }
    public string ErrorCode { get; set; }
    public Data Data { get; set; }
}

Hope it helps!



来源:https://stackoverflow.com/questions/42479947/cannot-deserialize-the-current-json-object-into-type-system-collections-generic

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!