如何使用嵌套的对象数组反序列化JSON数组
本文关键字:数组 反序列化 JSON 对象 何使用 嵌套 | 更新日期: 2023-09-27 18:27:24
我有一个JSON字符串,如下所示,并希望对其进行反序列化:
[[{"campaignId":201410018,"programCode":"54321"}],[{"campaignId":201410018,"programCode":"54321"}]]
我创建了一些类如下:
public class Rootclass
{
public List<JSONResponse> rootClass { get; set; }
}
public class JSONResponse
{
public int campaignId { get; set; }
public string programCode { get; set; }
}
我调用这个JSON.NET方法来反序列化JSON:
List<Rootclass> myDeserializedObjList = (List<Rootclass>)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(List<Rootclass>));
但我得到了以下错误:
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.
我做错了什么?
JSON表示List<List<JSONResponse>>
,而不是List<RootClass>
。这样试试:
List<List<JSONResponse>> myDeserializedObjList =
JsonConvert.DeserializeObject<List<List<JSONResponse>>>(json);
Fiddle:https://dotnetfiddle.net/geRLdb