JSON Desrialize Dictionary

时间:2015-11-27 03:45:59

标签: c# json json.net

我试图反序列化这个json,最初的航空公司属性是一个IList,但这不起作用,因为值正在改变,所以我使用字典我可以得到密钥就好但值是空的。

这是我试图反序列化的样本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; }
}

下图显示的计数与上面的JSON对象计数完全相同,但是值为空,我还想得到航空公司名称的值。

enter image description here

空值:

enter image description here

1 个答案:

答案 0 :(得分:1)

Airlines课程没有必要。将您的媒体资源声明为Dictionary<string, AirlineDetails>

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