我有一个 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>>
, not a List<RootClass>
。试试这样:
List<List<JSONResponse>> myDeserializedObjList =
JsonConvert.DeserializeObject<List<List<JSONResponse>>>(json);
Fiddle: https://dotnetfiddle.net/geRLdb https://dotnetfiddle.net/geRLdb
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)