简体   繁体   English

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

[英]Deserializing JSON Object into C# list

I have some JSON that I want to deserilize into a list<> object. 我有一些要反序列化为list <>对象的JSON。 I am using JsonConvert by Newtonsoft and I have setup my public classes to support the list. 我正在使用Newtonsoft的JsonConvert,并且已经设置了公共类来支持该列表。 These are as follows. 这些如下。

        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; }
    }

The JSON is as follows. 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"
}]}

To deserialize the JSON I have the following line. 要反序列化JSON,我需要以下几行。

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

This is where it's falling over. 这就是它倒塌的地方。 The JsonConvertor is throwing and exception. JsonConvertor抛出异常。

Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List`1[ContractExpressAPITest.Form1+NewDocumentObject]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. 无法反序列化当前JSON对象(例如{“ name”:“ value”})为类型'System.Collections.Generic.List`1 [ContractExpressAPITest.Form1 + NewDocumentObject]',因为该类型需要JSON数组(例如[1, 2,3])正确反序列化。 To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. 要解决此错误,可以将JSON更改为JSON数组(例如[1,2,3]),也可以更改反序列化类型,使其成为普通的.NET类型(例如,不像整数这样的原始类型,也不像这样的集合类型。数组或列表),可以从JSON对象反序列化。 JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. 还可以将JsonObjectAttribute添加到类型中,以强制其从JSON对象反序列化。 Path 'ContractId', line 1, position 14. 路径“ ContractId”,第1行,位置14。

I suspect this is because it's not able to handle the List items in the JSON. 我怀疑这是因为它无法处理JSON中的List项。

Anyone able to help?? 有谁能帮忙?

Thanks 谢谢

Copy your JSON to the clipboard and then if your are using Visual Studio in the "Edit> Paste Special> Paste JSON as classes." 将JSON复制到剪贴板,然后在“编辑”>“选择性粘贴”>“将JSON粘贴为类”中使用Visual Studio。 and then do the same 然后做同样的

You are trying to deserialize into a List 您正在尝试反序列化为列表

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

But your JSON string contains an object {} only. 但是您的JSON字符串仅包含对象{}

Change your JSON input to [{...}] Or change your deserialize call to only one object. 将您的JSON输入更改为[{...}]或将反序列化调用更改为仅一个对象。

You cannot serialize or deserialize a List of <object> . 您不能序列化或反序列化<object>的列表。 You must use a strongly typed class. 您必须使用强类型的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM