Deserializing JSON Object into C# list

流过昼夜 提交于 2019-12-23 03:04:39

问题


I have some JSON that I want to deserilize into a list<> object. I am using JsonConvert by Newtonsoft and I have setup my public classes to support the list. 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.

{
"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.

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

This is where it's falling over. The JsonConvertor is throwing and exception.

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ContractExpressAPITest.Form1+NewDocumentObject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'ContractId', line 1, position 14.

I suspect this is because it's not able to handle the List items in the JSON.

Anyone able to help??

Thanks


回答1:


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.

Change your JSON input to [{...}] Or change your deserialize call to only one object.




回答2:


Copy your JSON to the clipboard and then if your are using Visual Studio in the "Edit> Paste Special> Paste JSON as classes." and then do the same




回答3:


You cannot serialize or deserialize a List of <object>. You must use a strongly typed class.



来源:https://stackoverflow.com/questions/35481066/deserializing-json-object-into-c-sharp-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!