简体   繁体   English

如何使用嵌套对象数组反序列化JSON数组

[英]How to deserialize a JSON array with nested arrays of objects

I have a JSON string as follows and want to deserialize it: 我有一个JSON字符串如下,并想反序列化它:

[[{"campaignId":201410018,"programCode":"54321"}],[{"campaignId":201410018,"programCode":"54321"}]]

I have created some classes as follows: 我创建了一些类,如下所示:

public class Rootclass
{
    public List<JSONResponse> rootClass { get; set; }
}

public class JSONResponse
{
    public int campaignId { get; set; }
    public string programCode { get; set; }
}

I am calling this JSON.NET method to deserialize the JSON: 我正在调用此JSON.NET方法来反序列化JSON:

List<Rootclass> myDeserializedObjList = (List<Rootclass>)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(List<Rootclass>));

But I am getting the error below: 但是我收到以下错误:

Cannot deserialize JSON array (i.e. [1,2,3]) into type 'JSON_Test.Rootclass'.
The deserialized type must be an array or implement a collection interface like IEnumerable, ICollection or IList.

What am I doing wrong? 我究竟做错了什么?

Your JSON represents a List<List<JSONResponse>> , not a List<RootClass> . 您的JSON表示一个List<List<JSONResponse>> ,而不是List<RootClass> Try it like this: 像这样尝试:

List<List<JSONResponse>> myDeserializedObjList = 
    JsonConvert.DeserializeObject<List<List<JSONResponse>>>(json);

Fiddle: https://dotnetfiddle.net/geRLdb 小提琴: https : //dotnetfiddle.net/geRLdb

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

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