遇到具有相同名稱的反序列化對象時出現問題。它們可以重複,但是它們沒有以數組的形式格式化,所以我使用的Newtonsoft.Json庫不允許我從這些對象創建數組。這裏是我面對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」對象作爲數組?
當反序列化JSON字符串時會出現問題。我收到一個錯誤:「無法將屬性TESKO添加到Newtonsoft.Json.Linq.JObject。具有相同名稱的屬性已經存在於對象上。」 – AlexProutorov
好的。答案已更新。一探究竟。 JObject.Parse應該工作。 – acbarberi
仍然出現錯誤「無法將當前JSON對象反序列化爲類型'Test.TESKO []',因爲該類型需要JSON數組(例如[1,2,3])才能正確反序列化。」這意味着JSON輸入字符串與數組類型不匹配,它的格式不正確 – AlexProutorov