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对象中,我们有相同的结构和不同的对象名称。目的是将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>来正确地反序列化?

更新

给定更新的类和更新的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
    }
  }

正如您所看到的,所有数据都已成功反序列化和序列化。

尝试这样的数据结构-

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反序列化为该CCD_ 14类。

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

相关内容

  • 没有找到相关文章

最新更新