我正在尝试反序列化此JSON,但我一直在收到错误。有人可以帮帮我吗?我在哪里弄错了?
JSON:
{
"totalItems": 63,
"items": [
{
"id": 100039812,
"group": {
"code": "DD",
"description": "Delivery Documents"
},
"type": {
"code": "READ",
"description": "Logs"
},
"reference": "ARLDR",
"date": "2015-03-24T00:00:00",
"description": "ALogs",
"notes": "",
"lastUpdateDate": "2015-03-24T14:06:42.063",
"location": "BOX A001",
"metadata": {}
},
{
"id": 100039813,
"group": {
"code": "DD",
"description": "Delivery Documents"
},
"type": {
"code": "BL",
"description": "Logbooks"
},
"reference": "BALB",
"date": "2015-03-24T00:00:00",
"description": "Logbooks",
"notes": "",
"lastUpdateDate": "2015-03-24T14:07:42.44",
"location": "BOX A001",
"metadata": {}
}
]
}
public class Documents
{
public int totalItems { get; set; }
public List<doc_items> items { get; set; }
}
public class doc_items
{
public int id { get; set; }
public List<group_items> group { get; set; }
public List<type_items> type { get; set; }
public string reference { get; set; }
public string date { get; set; }
public string description { get; set; }
public string notes { get; set; }
public string lastUpdateDate { get; set; }
public string location { get; set; }
public List<metadata_list> metadata { get; set; }
}
public class group_items
{
public string code { get; set; }
public string description { get; set; }
}
public class type_items
{
public string code { get; set; }
public string description { get; set; }
}
public class metadata_list
{
}
然后我称之为:
Documents myDocuments = JsonConvert.DeserializeObject<Documents>(responsetext.ToString());
并收到以下错误:
错误:Newtonsoft.Json.JsonSerializationException: 无法将当前JSON对象(例如{&#34; name&#34;:&#34; value&#34;})反序列化为类型System.Collections.Generic.List`1 [AerData.Ranorex.Web.Records.API_Documents +文献]&#39;因为类型需要JSON数组(例如[...
)
答案 0 :(得分:1)
相反&#39; group&#39;,&#39; type&#39;和元数据&#39;不是数组,所以将您的类修改为:
public class doc_items
{
public int id { get; set; }
public group_items group { get; set; }
public type_items type { get; set; }
public string reference { get; set; }
public string date { get; set; }
public string description { get; set; }
public string notes { get; set; }
public string lastUpdateDate { get; set; }
public string location { get; set; }
public metadata_list metadata { get; set; }
}
答案 1 :(得分:0)
根据Documents
类的定义,group
的{{1}}属性必须是数组。在你给定的JSON中它是一个对象。同样适用于doc_items
属性
答案 2 :(得分:0)
我建议使用这样的转换器:http://json2csharp.com/#
试试这个:
class Program
{
static void Main(string[] args)
{
var sr = new StreamReader("json.json");
var jsonText = sr.ReadToEnd();
Documents myDocuments = JsonConvert.DeserializeObject<Documents>(jsonText);
}
}
public class group_items
{
public string code { get; set; }
public string description { get; set; }
}
public class type_items
{
public string code { get; set; }
public string description { get; set; }
}
public class metadata_list
{
}
public class doc_items
{
public int id { get; set; }
public group_items GroupItems { get; set; }
public type_items TypeItems { get; set; }
public string reference { get; set; }
public string date { get; set; }
public string description { get; set; }
public string notes { get; set; }
public string lastUpdateDate { get; set; }
public string location { get; set; }
public metadata_list MetadataList { get; set; }
}
public class Documents
{
public int totalItems { get; set; }
public List<doc_items> items { get; set; }
}
您的数据文件json.json
{
"totalItems": 63,
"items": [
{
"id": 100039812,
"group": {
"code": "DD",
"description": "Delivery Documents"
},
"type": {
"code": "READ",
"description": "Logs"
},
"reference": "ARLDR",
"date": "2015-03-24T00:00:00",
"description": "ALogs",
"notes": "",
"lastUpdateDate": "2015-03-24T14:06:42.063",
"location": "BOX A001",
"metadata": { }
},
{
"id": 100039813,
"group": {
"code": "DD",
"description": "Delivery Documents"
},
"type": {
"code": "BL",
"description": "Logbooks"
},
"reference": "BALB",
"date": "2015-03-24T00:00:00",
"description": "Logbooks",
"notes": "",
"lastUpdateDate": "2015-03-24T14:07:42.44",
"location": "BOX A001",
"metadata": { }
}
]
}