2015-03-03 50 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

相關問題