2015-03-03 49 views
0

我使用Json.net从存在服务反序列化JSON数据。 样本:Json.net反序列化相同objectcts不同名称

{ 
    "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是反序列化containersContainer对象的数组:

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>正确地反序列化?

+0

'containers'属性是泛型类型吗?请详细说明你的课程。 – 2015-03-03 00:24:21

+0

我不知道它是通用的服务器上,但在我的C#实现它不是通用的。 – user2598575 2015-03-03 00:27:08

+1

这是错别字对不起。我已修复 – user2598575 2015-03-03 00:40:45

回答

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

注意,"root"财产的JSON需要额外的间接级别,这是我们通过RootObject类提供的。你可能会想你的Root重命名为更具描述性的像RootContainer

更新2

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

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

原来的答案

假设你的嵌套containers也是Container类型,你可以直接de它们序列作为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

尝试的数据结构是这样 -

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); 
+0

这不起作用('容器'字典为空 – user2598575 2015-03-03 00:39:09

+0

我能够使其完美工作。 – Muctadir 2015-03-03 00:41:58

+0

对不起,我在我的代码中输入了一个类型。你很棒。有用! – user2598575 2015-03-03 00:48:32

相关问题