1

I am using Json.net to deserialize JSON data from exist service. The sample:

{
    "id" : 3223,
    "name" : "position 3223",
    "containers" : {
        "container_demo" : {
            "id" : 12,
            "name" : "demo",
            "value" : 34
        },
        "different_name_1" : {
            "id" : 33,
            "name" : "demo 3",
            "value" : 1
        },
        "another_contaier" : {
            "id" : 1,
            "name" : "another demo",
            "value" : 34
        }
    }
}

As we see, we have same structure and different names of objects in container object. The taget is to deserialize containers as Array of Container objects:

public class Root
{
    public int id {set;get;}
    public string name {set;get;}
    Array<Container> containers {set;get;}
}

public class Container
{
    public int id {set; get;}
    public string name {set;get;}
    public int value {set;get;}
}

How to solve it? Is it possible to use CustomCreationConverter<T> to deserialize this correctly?

3
  • The containers property is a generic type? Detail your classes, please. Commented Mar 3, 2015 at 0:24
  • I do not know is it generic on server, but in my C# implementation it is not generic. Commented Mar 3, 2015 at 0:27
  • 1
    it is typo sorry. i have fixed Commented Mar 3, 2015 at 0:40

2 Answers 2

4

Update

Given your updated classes and updated JSON, you would do:

public class RootObject
{
    public Root root { get; set; }
}

public class Root
{
    public int id {set;get;}
    public string name {set;get;}
    public Dictionary<string, Container> containers { get; set; }
}

public class Container
{
    public int id {set; get;}
    public string name {set;get;}
    public int value {set;get;}
}

And use it like:

        var root = JsonConvert.DeserializeObject<RootObject>(json);

Note that the "root" property in the JSON requires an extra level of indirection, which us provided by the RootObject class. You might want to rename your Root to something more descriptive like RootContainer.

Update 2

The JSON in the question was modified again to eliminate the "root" property, so RootObject is unnecessary and you just need to do:

        var root = JsonConvert.DeserializeObject<Root>(json);

Original Answer

Assuming your nested containers are also of type Container, you can just deserialize them as a Dictionary<string, Container> property:

public class Container
{
    public int id { set; get; }
    public string name { set; get; }
    public int? value { set; get; } // Null when the property was not present in the JSON.
    public Dictionary<string, Container> containers { get; set; }
}

You would use it like:

    public static void Test()
    {
        string json = @"
        {
            ""id"" : 3223,
            ""name"" : ""position 3223"",
            ""containers"" : {
                ""container_demo"" : {
                    ""id"" : 12,
                    ""name"" : ""demo"",
                    ""value"" : 34
                },
                ""different_name_1"" : {
                    ""id"" : 33,
                    ""name"" : ""demo 3"",
                    ""value"" : 1
                },
                ""another_contaier"" : {
                    ""id"" : 1,
                    ""name"" : ""another demo"",
                    ""value"" : 34
                }
            }
        }";

        var container = JsonConvert.DeserializeObject<Container>(json);

        JsonSerializerSettings settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };

        Debug.WriteLine(JsonConvert.SerializeObject(container, Formatting.Indented, settings));
    }

This produces the output:

{
  "id": 3223,
  "name": "position 3223",
  "containers": {
    "container_demo": {
      "id": 12,
      "name": "demo",
      "value": 34
    },
    "different_name_1": {
      "id": 33,
      "name": "demo 3",
      "value": 1
    },
    "another_contaier": {
      "id": 1,
      "name": "another demo",
      "value": 34
    }
  }

As you can see, all data was deserialized and serialized successfully.

Sign up to request clarification or add additional context in comments.

Comments

1

Try a data structure like this -

public class Holder
{
    public int id { set; get; }

    public string name { set; get; }

    public Dictionary<string, Container> containers{ get; set; }
}


public class Container
{
    public int id { set; get; }

    public string name { set; get; }

    public int value { set; get; }
}

and deserialize json to this Holder class.

var holders = JsonConvert.DeserializeObject<Holder> (json);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.