0

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?

AlexProutorov
  • 677
  • 11
  • 22

2 Answers2

1

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?

Community
  • 1
  • 1
acbarberi
  • 79
  • 8
  • the problem occurs when deserializing JSON string. I get an error "Can not add property TESKO to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object." – AlexProutorov Nov 01 '13 at 16:18
  • still getting an error "Cannot deserialize the current JSON object into type 'Test.TESKO[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly." which means that JSON input string doesn't match array type, it is not formatted correctly – AlexProutorov Nov 01 '13 at 16:31
  • Update the original post to include the C# data type you are trying to deserialize into. – acbarberi Nov 01 '13 at 16:40
  • Check out the update. Something has to change on the server or you may have to write your own deserializer. – acbarberi Nov 01 '13 at 18:27
0

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"
        }
    }
],
Esteban Elverdin
  • 3,552
  • 1
  • 17
  • 21
  • yes! but that's my problem! I cannot influence on how JSON string is built. That's the only format I can get from web service and I have to deal with it – AlexProutorov Nov 01 '13 at 16:22