[英]How to deserialize a JSON array with nested arrays of objects
我有一個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);
小提琴: https : //dotnetfiddle.net/geRLdb
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.