2016-02-10 111 views
1

我已經在我的JSON文件反序列化與JSON文件,內部類與JSON.NET C#

"missionName": "missionname", 
    "thumb_image": "pics/mission-info.png", 
    "uplinkPage": [ 
    { 
     "RPC": { 
      "name": "RPC", 
      "rpc": "", 
      "args": "" 
     }, 
     "EXE": { 
      "name": "app1", 
      "prog": "", 
      "args": "" 
     }, 
     "VM": { 
      "name": "VM", 
      "name": "", 
      "args": "" 
     }, 
     "JAR": { 
      "name": "JAR", 
      "prog": "", 
      "args": "" 
     }, 
     "link": { 
      "name": "somelink", 
      "url": "" 
     } 
    } 
], 

以下結構爲了這個,我有下面的類

public class EXE 
{ 
    public string name { get; set; } 
    public string prog { get; set; } 
    public string args { get; set; } 
} 

public class RPC 
{ 
    public string name { get; set; } 
    public string rpc { get; set; } 
    public string args { get; set; } 
} 

public class VM 
{ 
    public string name { get; set; } 
    public string args { get; set; } 
} 

public class JAR 
{ 
    public string name { get; set; } 
    public string prog { get; set; } 
    public string args { get; set; } 
} 

public class Link 
{ 
    public string name { get; set; } 
    public string url { get; set; } 
} 

public class UplinkPage 
{ 
    public VM[] vmList { get; set; } 
    public EXE[] exeList { get; set; } 
    public RPC[] rpcList { get; set; } 
    public JAR[] jarList { get; set; } 
    public Link[] linkList { get; set; } 
} 

public class Rootobject 
{ 
    public string missionName { get; set; } 
    public string thumb_image { get; set; }  
    public Uplinkpage[] uplinkPage { get; set; }   
} 

的uplinkPage部分可以每個EXE,RPC,VM ..部分都有一個或多個。我嘗試添加乘部分這樣

"EXE": { 
      "1": { 
       "name": "app1", 
       "data-prog": "", 
       "data-args": "" 
      }, 
      "2": { 
       "name": "app2", 
      "data-prog": "", 
      "data-args": "" 
      } 
     } 

當我做反序列化作爲這樣

Rootobject page = JsonConvert.DeserializeObject<Rootobject>(File.ReadAllText("mission1.json")); 

我得到一個對象,我可以讀,除非我有相同類型的多重價值的一切,我只會得到其中一個。如果我宣佈部分作爲陣列

public EXE[] exeList { get; set; } 

我將獲得最後一個,如果作爲

Dictionary<string,EXE> exeList {get; set;} 

我會得到的第一個。我注意到,當我使用字典時,EXE的類型更改爲EXE1。

所以我想知道我在做什麼錯了?我在這裏執行一些任務嗎?

歡呼聲, ES

+0

您是否可以控制JSON的格式? – dbc

+1

是的,我完全控制它,但我認爲我使用的是不正確的。 – Goozo

回答

1

每當你有可能被複制一個JSON屬性,這將是最簡單的表示屬性的值作爲JSON array,而不是重複名稱的多個特性。即而不是

{ 
    "EXE" : {"id":1}, 
    "RPC" : {"name":"a"},   
    "EXE" : {"id":2}, 
} 

你想做的事:

{ 
    "EXE" : [{"id":1}, {"id":2}], 
    "RPC" : [{"name":"a"}]   
} 

同樣,在​​類,name出現多次,所以name應該是一個數組,以及:

public class VM 
{ 
    public string [] name { get; set; } 
    public string args { get; set; } 
} 

(這不是不可能反序列化重複的屬性名稱,只是很難,而且需要是一個自定義轉換器。請參閱How to deserialize JSON with duplicate property names in the same object。既然你控制了你的JSON格式,我建議避免這種情況。在您的問題中使用帶索引屬性的嵌套對象也是一種選擇;它不那麼困難,但仍然需要自定義轉換。見How to parse this JSON using Newton Soft for nested object。但是使用JSON數組最容易。)

接下來,您需要告訴Json.NET如何將c#屬性名稱映射到JSON屬性名稱,當它們不一致時。例如,在你的JSON中,你有一個屬性"EXE",但在c#中屬性名稱是public EXE[] exeList { get; set; }。您可以重命名JSON性能,重命名C#的屬性,也不能使用[JsonProperty]映射:我還你EXE對象有時具有"data-prog"財產注意到

public class UplinkPage 
{ 
    [JsonProperty("VM")] 
    public VM[] vmList { get; set; } 

    [JsonProperty("EXE")] 
    public EXE[] exeList { get; set; } 

    [JsonProperty("RPC")] 
    public RPC[] rpcList { get; set; } 

    [JsonProperty("JAR")] 
    public JAR[] jarList { get; set; } 

    [JsonProperty("link")] 
    public Link[] linkList { get; set; } 
} 

,有時只是一個"prog"。你應該在一個標準化。

因此,你的JSON應該是這樣的:

{ 
    "missionName": "missionname", 
    "thumb_image": "pics/mission-info.png", 
    "uplinkPage": [ 
    { 
     "RPC": [ 
     { 
      "name": "RPC", 
      "rpc": "", 
      "args": "" 
     } 
     ], 
     "EXE": [ 
     { 
      "name": "app1", 
      "prog": "prog1", 
      "args": "args1" 
     }, 
     { 
      "name": "app2", 
      "prog": "prog2", 
      "args": "args2" 
     } 
     ], 
     "VM": [ 
     { 
      "name": [ 
      "VM", 
      "" 
      ], 
      "args": "" 
     } 
     ], 
     "JAR": [ 
     { 
      "name": "JAR", 
      "prog": "", 
      "args": "" 
     } 
     ], 
     "link": [ 
     { 
      "name": "somelink", 
      "url": "" 
     } 
     ] 
    } 
    ] 
} 

而且你的類應該是這樣的:

public class EXE 
{ 
    public string name { get; set; } 
    public string prog { get; set; } 
    public string args { get; set; } 
} 

public class RPC 
{ 
    public string name { get; set; } 
    public string rpc { get; set; } 
    public string args { get; set; } 
} 

public class VM 
{ 
    public string [] name { get; set; } 
    public string args { get; set; } 
} 

public class JAR 
{ 
    public string name { get; set; } 
    public string prog { get; set; } 
    public string args { get; set; } 
} 

public class Link 
{ 
    public string name { get; set; } 
    public string url { get; set; } 
} 

public class UplinkPage 
{ 
    [JsonProperty("VM")] 
    public VM[] vmList { get; set; } 

    [JsonProperty("EXE")] 
    public EXE[] exeList { get; set; } 

    [JsonProperty("RPC")] 
    public RPC[] rpcList { get; set; } 

    [JsonProperty("JAR")] 
    public JAR[] jarList { get; set; } 

    [JsonProperty("link")] 
    public Link[] linkList { get; set; } 
} 

public class Rootobject 
{ 
    public string missionName { get; set; } 
    public string thumb_image { get; set; } 
    public UplinkPage[] uplinkPage { get; set; } 
} 

原型fiddle

+0

謝謝,學到了很多:-) – Goozo