Json.net反序列化具有不同名称的相同对象

时间:2015-03-03 00:17:51

标签: c# serialization json.net

我正在使用Json.net从现有服务反序列化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
        }
    }
}

如我们所见,container对象中的对象具有相同的结构和不同的名称。 taget将containers反序列化为Container对象的数组:

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;}
}

如何解决?是否可以使用CustomCreationConverter<T>对此进行反序列化?

2 个答案:

答案 0 :(得分:3)

<强>更新

鉴于您更新的课程和更新的JSON,您可以这样做:

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;}
}

并使用它:

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

请注意,JSON中的"root"属性需要额外的间接级别,我们由RootObject类提供。您可能希望将Root重命名为更具描述性的内容,例如RootContainer

更新2

问题中的JSON再次被修改以消除"root"属性,因此RootObject是不必要的,您只需要这样做:

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

原始答案

假设您的嵌套containers也是Container类型,您可以将它们反序列化为Dictionary<string, Container>属性:

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; }
}

您可以使用它:

    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));
    }

这会产生输出:

{
  "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
    }
  }

如您所见,所有数据都已反序列化并成功序列化。

答案 1 :(得分:1)

尝试这样的数据结构 -

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; }
}

并将json反序列化为此Holder类。

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