[英]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>
,库将处理从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"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.