【发布时间】:2016-05-30 14:17:24
【问题描述】:
我有一些 JSON 想要反序列化为 list 对象。 我正在使用 Newtonsoft 的 JsonConvert,并且我已经设置了我的公共类来支持该列表。这些如下。
public class NewDocumentObject
{
public int ContractId { get; set; }
public int FolderId { get; set; }
public string CreatedAt { get; set; }
public string CreatedBy { get; set; }
public string TemplateReference { get; set; }
public bool IsTest { get; set; }
public bool IsExplicitName { get; set; }
public object Requester { get; set; }
public object ExternalReference { get; set; }
public object ExternalLabel { get; set; }
public string Status { get; set; }
public string StatusLabel { get; set; }
public int Share { get; set; }
public int EffectiveRight { get; set; }
public string Name { get; set; }
public object ModifiedAt { get; set; }
public object ModifiedBy { get; set; }
public object ModifiedById { get; set; }
public object ProfileReference { get; set; }
public object ESignatureId { get; set; }
public List<object> Documents { get; set; }
public object Folder { get; set; }
public object Session { get; set; }
public string ESignatureStatus { get; set; }
public List<object> Alerts { get; set; }
public List<Link> Links { get; set; }
}
public class Link
{
public string rel { get; set; }
public string method { get; set; }
public string href { get; set; }
}
JSON 如下。
{
"ContractId": 103,
"FolderId": 6,
"CreatedAt": "2016-02-18T11:30:17.293",
"CreatedBy": "SMTC",
"TemplateReference": "Non Disclosure Agreement",
"IsTest": false,
"IsExplicitName": false,
"Requester": null,
"ExternalReference": null,
"ExternalLabel": null,
"Status": "Incomplete",
"StatusLabel": "Incomplete",
"Share": 0,
"EffectiveRight": 3,
"Name": "Non Disclosure Agreement",
"ModifiedAt": null,
"ModifiedBy": null,
"ModifiedById": null,
"ProfileReference": null,
"ESignatureId": null,
"Documents": [],
"Folder": null,
"Session": null,
"ESignatureStatus": "",
"Alerts": [],
"Links": [{
"rel": "questionnaire",
"method": "get",
"href": "http://srv-dev-29/api/contracts/103/questionnaire/pages/1?navigate=first"
}, {
"rel": "answers",
"method": "get",
"href": "http://srv-dev-29/api/contracts/103/answers"
}, {
"rel": "documents",
"method": "get",
"href": "http://srv-dev-29/api/contracts/103/documents"
}, {
"rel": "template",
"method": "get",
"href": "http://srv-dev-29/api/templates/Non Disclosure Agreement"
}, {
"rel": "folder",
"method": "get",
"href": "http://srv-dev-29/api/folders/6"
}, {
"rel": "self",
"method": "get",
"href": "http://srv-dev-29/api/contracts/103"
}]}
要反序列化 JSON,我有以下行。
List<NewDocumentObject> newDoc = JsonConvert.DeserializeObject<List<NewDocumentObject>>(response.Content.ReadAsStringAsync().Result);
这就是它倒下的地方。 JsonConvertor 正在抛出异常。
无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[ContractExpressAPITest.Form1+NewDocumentObject]',因为该类型需要 JSON 数组(例如[1,2,3]) 正确反序列化。 要修复此错误,要么将 JSON 更改为 JSON 数组(例如 [1,2,3]),要么将反序列化类型更改为普通的 .NET 类型(例如,不是像整数这样的原始类型,而不是像这样的集合类型可以从 JSON 对象反序列化的数组或列表。 JsonObjectAttribute 也可以添加到类型中以强制它从 JSON 对象反序列化。 路径“ContractId”,第 1 行,位置 14。
我怀疑这是因为它无法处理 JSON 中的列表项。
谁能帮忙??
谢谢
【问题讨论】:
标签: c# arrays json serialization