1

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

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

That's how I generated my classes in the first place.
Wow, I didn't know this feature existed! It will help me a lot when parsing JSONs, thank you!
2

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.

3 Comments

I added the square brackets to the JSON but it's still throwing the same error. I suspect I am being a bit thick with this. I have another list<> for some different JSON that is working perfectly. That is a list of 7 objects. However this is just 1 item with a list of links.
<idiot>Actually ignore me the Square brackets are working perfecty.</idiot>
"Or change your deserialize call to only one object." how can we do this ?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.