gpt4 book ai didi

c# - 将 JSON 对象反序列化为 C# 列表

转载 作者:太空宇宙 更新时间:2023-11-03 21:14:19 24 4
gpt4 key购买 nike

我有一些 JSON,我想将其反序列化为 list<> 对象。我正在使用 Newtonsoft 的 JsonConvert,并且我已经设置了我的公共(public)类来支持该列表。这些如下。

        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 中的列表项。

有人能帮忙吗??

谢谢

最佳答案

将您的 JSON 复制到剪贴板,然后如果您在“编辑 > 选择性粘贴 > 将 JSON 作为类粘贴”中使用 Visual Studio。然后做同样的事情

关于c# - 将 JSON 对象反序列化为 C# 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35481066/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com