C# Json.net反序列化具有不同名称的相同ObjectCT

C# Json.net反序列化具有不同名称的相同ObjectCT,c#,serialization,json.net,C#,Serialization,Json.net,我使用Json.net从exist服务反序列化Json数据。 样本: { "id" : 3223, "name" : "position 3223", "containers" : { "container_demo" : { "id" : 12, "name" : "demo", "value" : 34 }, "different_name_1" :

我使用Json.net从exist服务反序列化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
object中,对象具有相同的结构和不同的名称。 标记用于将
容器
反序列化为
容器
对象的数组:

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;}
}
公共类根目录
{
公共int id{set;get;}
公共字符串名称{set;get;}
数组容器{set;get;}
}
公营货柜
{
公共int id{set;get;}
公共字符串名称{set;get;}
公共int值{set;get;}
}

如何解决?是否可以使用
CustomCreationConverter
来正确地反序列化此文件?

更新

考虑到更新的类和更新的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;}
}
原始答案

假设嵌套的
容器
也是
容器
类型,您可以将它们反序列化为
字典
属性:

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

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

更新

考虑到更新的类和更新的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;}
}
原始答案

假设嵌套的
容器
也是
容器
类型,您可以将它们反序列化为
字典
属性:

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

尝试这样的数据结构-

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

containers
属性是泛型类型吗?请详细说明您的类。我不知道它在服务器上是通用的,但在我的C#实现中它不是通用的。很抱歉,它是打字错误。我已修复
容器
属性是否为泛型类型?请详细说明您的类。我不知道它在服务器上是通用的,但在我的C#实现中它不是通用的。很抱歉,它是打字错误。我已经修好了
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; }
}
var holders = JsonConvert.DeserializeObject<Holder> (json);