具有相同名称的反序列化对象有问题。它们可以重复,但是它们没有以数组形式格式化,因此我使用的 Newtonsoft.Json 库不允许我从这些对象创建数组。这是我遇到的 JSON 示例:
{
"TESKO": {
"Id": "19337",
"Name": "PR3337",
"Status": "Sold",
"Code": "GPPD",
"LastUpdatedDate": "2013-08-16",
"internalId": "19337"
},
"TESKO": {
"Id": "19337",
"Name": "PR-6477",
"Status": "Sold",
"Code": "GPPD",
"LastUpdatedDate": "2013-08-16",
"internalId": "19337"
},
"BRITISHTOBACCO": {
"Id": "19337",
"Name": "PR-4634",
"Status": "Sold",
"Code": "GPPD",
"LastUpdatedDate": "2013-08-16",
"internalId": "19337"
},
"DDI": {
"Id": "19337",
"Name": "PR-6477",
"Status": "Sold",
"Code": "GPPD",
"LastUpdatedDate": "2013-08-16",
"internalId": "19FF337"
}}
upd:这是我将 JSON 字符串反序列化为的类:
// Generated by Xamasoft JSON Class Generator
// http://www.xamasoft.com/json-class-generator
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ConsoleApplication2
{
public class MyResponse
{
[JsonProperty("TESKO")]
public TESKO[] TESKO { get; set; }
[JsonProperty("BRITISHTOBACCO")]
public BRITISHTOBACCO[] BRITISHTOBACCO { get; set; }
[JsonProperty("DDI")]
public DDI[] DDI { get; set; }
}
public class TESKO : CommonResult
{ }
public class BRITISHTOBACCO : CommonResult
{ }
public class DDI : CommonResult
{ }
public class TP : CommonResult
{ }
public class CommonResult
{
[JsonProperty("Id")]
public string Id { get; set; }
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty("Status")]
public string Status { get; set; }
[JsonProperty("Code")]
public string Code { get; set; }
[JsonProperty("LastUpdatedDate")]
public string LastUpdatedDate { get; set; }
[JsonProperty("internalId")]
public string InternalId { get; set; }
}
}
如何让反序列化器将“TESKO”对象视为数组?