简体   繁体   English

无法将json解析为对象

[英]Unable to parse json into an object

I am not sure how to parse the following json into a strongly typed object. 我不知道如何将以下json解析为强类型对象。

The JSON: JSON:

{
  "data": {
    "71161": {
      "air_by_date": 0,
      "anime": 0,
      "cache": {
        "banner": 1,
        "poster": 1
      },
      "indexerid": 71161,
      "language": "en",
      "network": "CBS",
      "next_ep_airdate": "",
      "paused": 0,
      "quality": "SD",
      "show_name": "name",
      "sports": 0,
      "status": "Ended",
      "subtitles": 0,
      "tvdbid": 71161
    },
    "71211": {
      "air_by_date": 0,
      "anime": 0,
      "cache": {
        "banner": 1,
        "poster": 1
      },
      "indexerid": 71211,
      "language": "en",
      "network": "ABC (US)",
      "next_ep_airdate": "",
      "paused": 0,
      "quality": "SD",
      "show_name": "name2",
      "sports": 0,
      "status": "Ended",
      "subtitles": 0,
      "tvdbid": 71211
    },
}

The issue is the number 71161 this can be different for each JSON response. 问题是数字71161 ,每个JSON响应可能会有所不同。

Use Json.NET from Newtonsoft . 使用Newtonsoft的Json.NET Let the data property be a Dictionary<int, Item> , the library will handle the conversion of keys from string to int : data属性为Dictionary<int, Item> ,库将处理从stringint的键转换:

class Program
{
    class Item
    {
        [JsonProperty(PropertyName = "status")]
        public string Status { get; set; }
    }
    class Root
    {
        [JsonProperty(PropertyName = "data")]
        public Dictionary<int, Item> Data { get; set; }
    }

    static void Main(string[] args)
    {
        var root = JsonConvert.DeserializeObject<Root>(@"{ ""data"": { ""123"": { ""status"": ""Ended"" } } }");
        Console.WriteLine(root.Data[123].Status); // prints "Ended"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM