2015-11-27 89 views
1

我試圖反序列化這個JSON,最初的航空公司屬性是一個IList,但這是行不通的,因爲值正在改變,所以我用字典我可以得到的關鍵就好了,但值是空的。JSON Desrialize字典

這是樣品JSON我試圖反序列化

"airlines": { 
     "MH": { 
     "lowcost": true, 
     "name": "Jetstar Asia", 
     "alliance_name": null, 
     "id": 300, 
     "rates": "18", 
     "average_rate": "3.12" 
     }, 
     "PR": { 
     "lowcost": true, 
     "name": "Tigerair", 
     "alliance_name": null, 
     "id": 800, 
     "rates": "26", 
     "average_rate": "3.41" 
     }, 
     "CZ": { 
     "lowcost": false, 
     "name": "Singapore Airlines", 
     "alliance_name": "Star Alliance", 
     "id": 449, 
     "rates": "620", 
     "average_rate": "4.18" 
     }, 
     "SQ": { 
     "lowcost": true, 
     "name": "AirAsia Zest", 
     "alliance_name": null, 
     "id": 839, 
     "rates": "104", 
     "average_rate": "2.56" 
     }, 
     "GA": { 
     "lowcost": true, 
     "name": "CEBU Pacific Air", 
     "alliance_name": null, 
     "id": 169, 
     "rates": "72", 
     "average_rate": "3.44" 
     }, 

這是屬性

下面
[JsonProperty("airlines")] 
public Dictionary<string, Airlines> Airlines { get; set; } 

public class Airlines 
{ 
    [JsonProperty("AirlineDetails")] 
    public ICollection<AirlineDetails> AirlineDetails { get; set; } 
} 

public class AirlineDetails 
{ 
    [JsonProperty("average_rate")] 
    public string AverageRate { get; set; } 

    [JsonProperty("alliance_name")] 
    public string AllianceName { get; set; } 

    [JsonProperty("name")] 
    public string Name { get; set; } 

    [JsonProperty("lowcost")] 
    public bool Lowcost { get; set; } 

    [JsonProperty("id")] 
    public int Id { get; set; } 

    [JsonProperty("rates")] 
    public string Rates { get; set; } 
} 

圖爲有一個計數是5完全一樣JSON對象計數上述,但價值是空的,我也想獲得這是航空公司的名稱的值。

enter image description here

空值:

enter image description here

回答

1

Airlines類是沒有必要的。聲明你的財產爲Dictionary<string, AirlineDetails>

public Dictionary<string, AirlineDetails> Airlines { get; set; } 
+0

它的工作表示感謝。 – Nevi