【问题标题】:Deserializing JSON Object into C# list将 JSON 对象反序列化为 C# 列表
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 这就是我最初生成课程的方式。
    • 哇,我不知道有这个功能!在解析 JSON 时对我有很大帮助,谢谢!
    【解决方案2】:

    您正在尝试反序列化为列表

    List<NewDocumentObject> newDoc = JsonConvert.DeserializeObject<List<NewDocumentObject>>(response.Content.ReadAsStringAsync().Result);
    

    但您的 JSON 字符串仅包含一个对象 {}

    将您的 JSON 输入更改为 [{...}] 或者将您的反序列化调用更改为仅一个对象。

    【讨论】:

    • 我在 JSON 中添加了方括号,但它仍然抛出同样的错误。我怀疑我对这个有点厚。对于一些运行良好的不同 JSON,我有另一个列表。那是 7 个对象的列表。然而,这只是一个带有链接列表的项目。
    • 实际上忽略我方括号工作得很好。
    • "或者将您​​的反序列化调用更改为仅一个对象。"我们该怎么做?
    【解决方案3】:

    您不能序列化或反序列化 &lt;object&gt; 的列表。您必须使用强类型类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-13
      • 2022-01-19
      • 1970-01-01
      • 2017-06-10
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      相关资源
      最近更新 更多