我不知道如何将下面的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>
,库将处理密钥从string
到int
:的转换
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"
}
}