反序列化JSON对象到C#列表 [英] Deserializing JSON Object into C# list

查看:172
本文介绍了反序列化JSON对象到C#列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些JSON,我想deserilize到一个列表<>对象。我使用JsonConvert由Newtonsoft
,我有我的设置公共类,支持列表中。这些都是如下:

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



的JSON如下:

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"
}]}

要反序列化JSON我有以下行。

To deserialize the JSON I have the following line.

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

这是它的掉落。该JsonConvertor抛出和异常

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

无法反序列化当前的JSON对象(如{名:值})入式'System.Collections.Generic.List `1 [ContractExpressAPITest.Form1 + NewDocumentObject]',因为类型需要一个JSON数组(如[1,2,3]),以正确的反序列化。
。要解决这个错误要么改变的JSON到一个JSON数组(如[1,2,3])或改变反序列化的类型,以便它是一个正常的.NET类型(例如,不是原始类型类似整数,而不是集合类型一样,可以从一个JSON对象反序列化数组或列表)。 JsonObjectAttribute也可以加入到类型迫使它从JSON对象反序列化。
路径ContractId,1号线,14位。

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.

我怀疑这是因为它无法处理的JSON列表中的项目。

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

任何人都能够帮助??

Anyone able to help??

感谢

推荐答案

您正试图反序列化到一个List

You are trying to deserialize into a List

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



但是,你的JSON字符串包含的对象 {} 只。

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

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

这篇关于反序列化JSON对象到C#列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆