Reputation: 707
Having troubles with deserialization objects with the same name. They can repeat however they are not formatted in a form of array so Newtonsoft.Json library that I use will not allow me to create arrays from these objects. Here is an example of JSON that I face with:
{
"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: Here is class that I deserialize my JSON string to:
// 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; }
}
}
How can I make deserializer treat 'TESKO' objects as arrays?
Upvotes: 0
Views: 1581
Reputation: 79
That JSON is fail. The JSON you are receiving is not formatted correctly.
Is there no way the people giving out the JSON on the server can change it so that it is formatted correcty? You do not have an array coming from the server. What you actually have is a property (in this case, "TESKO") being defined twice.
Look at this fail C# code...
String fail;
String fail;
You have the exact same thing with your JSON. It's like they are trying to define the variable TESKO twice.
Look at this code...
var js = "{\"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\"}}";
var items = JObject.Parse(js);
foreach (var i in items)
{
Response.Write("Key: " + i.Key + "<br/>Value: " + i.Value + "<br/><br/>");
}
Returns the following output:
Key: TESKO
Value: { "Id": "19337", "Name": "PR-6477", "Status": "Sold", "Code": "GPPD", "LastUpdatedDate": "2013-08-16", "internalId": "19337" }
Key: BRITISHTOBACCO
Value: { "Id": "19337", "Name": "PR-4634", "Status": "Sold", "Code": "GPPD", "LastUpdatedDate": "2013-08-16", "internalId": "19337" }
Key: DDI
Value: { "Id": "19337", "Name": "PR-6477", "Status": "Sold", "Code": "GPPD", "LastUpdatedDate": "2013-08-16", "internalId": "19FF337" }
The only option I see is to make a customer JSON deserializer...
How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?
Upvotes: 1
Reputation: 3582
If you want to deserialize TESKO as an array, then you json should look like this:
"TESKOS": [
{
"TESKO": {
"Id": "19337",
"Name": "PR-6477",
"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"
}
}
],
Upvotes: 0