簡體   English   中英

無法將json解析為對象

[英]Unable to parse json into an object

我不知道如何將以下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
    },
}

問題是數字71161 ,每個JSON響應可能會有所不同。

使用Newtonsoft的Json.NET 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