如何在 vb.net 中解析带有动态日期的 JSON 字符串?
How to Parse a JSON string with dynamic date in vb.net?
我正在尝试解析下面的这个 JSON 字符串,这样我就可以循环浏览来自 trakt 电视网站的每个节目和剧集,而不管日期如何。 json 字符串是实际的样本,只有一个日期 2015-01-01,但实际流将有更多日期。
所以我的问题是,在下面的 vbcode 中,我如何在不指定日期的情况下循环播放电视节目?
希望有人能提供帮助。 , 提前致谢!
VB:
Dim Reader As New StreamReader(client.OpenRead("http://api.trakt.tv/calendar/premieres.json/5708fdcd69c6c666f4cbcf369cb22584/" & Now.Year & Now.AddMonths(-1).ToString("MM") & "01/100"))
Dim dynamicObj = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Object)(res)
Dim premiers = dynamicObj("2015-01-01")
For Each subObj As Object In premiers
Console.WriteLine(subObj("show")("title"))
Next
JSON:
{
"2015-01-01" : [{
"airs_at" : "2015-01-01T00:00:00.000-08:00",
"episode" : {
"season" : 2015,
"number" : 1,
"title" : "01/01/2015",
"ids" : {
"trakt" : 1685096,
"tvdb" : 5090491,
"imdb" : null,
"tmdb" : null,
"tvrage" : null
},
"number_abs" : null,
"overview" : "It's the Red Eye 2015 Mildly Amusing New Years special with guests Mike Baker, Bonnie McFarlane and a special treat...a retrospective of Frightened Correspondent Joe Machi!",
"first_aired" : "2015-01-01T00:00:00.000-08:00",
"rating" : 8.0,
"votes" : 1,
"updated_at" : "2015-01-18T21:10:22.000Z",
"available_translations" : []
},
"show" : {
"title" : "Red Eye w/Greg Gutfeld",
"year" : 2007,
"ids" : {
"trakt" : 3612,
"slug" : "red-eye-w-greg-gutfeld",
"tvdb" : 268484,
"imdb" : "tt0964948",
"tmdb" : 3635,
"tvrage" : null
},
"overview" : "Red Eye w/Greg Gutfeld is an American late-night/early-morning satirical talk show on the Fox News Channel, airing at 3:00 am ET Tuesday through Saturday and 11:00 pm Saturday and 2:00 am Sunday. The show features panelists and guests discussing the latest news in politics, pop culture, entertainment, business, sports, and religion. The show is hosted by Greg Gutfeld, a self-described libertarian, who is a former Maxim UK editor.",
"first_aired" : "2007-02-06T00:00:00.000-08:00",
"airs" : {
"day" : "Thursday",
"time" : "03:00",
"timezone" : "America/New_York"
},
"runtime" : 60,
"certification" : "TV-14",
"network" : null,
"country" : "us",
"trailer" : null,
"homepage" : null,
"status" : "ended",
"rating" : 0.0,
"votes" : 0,
"updated_at" : "2015-01-29T10:44:57.000Z",
"language" : "en",
"available_translations" : ["en"],
"genres" : ["comedy"],
"aired_episodes" : 322
}
}, {
"airs_at" : "2015-01-01T10:00:00.000-08:00",
"episode" : {
"season" : 2015,
"number" : 1,
"title" : "01/01/2015",
"ids" : {
"trakt" : 1602414,
"tvdb" : 5087659,
"imdb" : null,
"tmdb" : null,
"tvrage" : null
},
"number_abs" : null,
"overview" : null,
"first_aired" : "2015-01-01T10:00:00.000-08:00",
"rating" : 0.0,
"votes" : 0,
"updated_at" : "2015-01-05T09:50:48.000Z",
"available_translations" : []
},
"show" : {
"title" : "BBC News at Six",
"year" : null,
"ids" : {
"trakt" : 3499,
"slug" : "bbc-news-at-six",
"tvdb" : 264761,
"imdb" : "",
"tmdb" : 3521,
"tvrage" : null
},
"overview" : "The BBC News at Six is the evening news programme broadcast each night on British television channel BBC One and the BBC News channel at 18:00.",
"first_aired" : "1984-09-02T17:00:00.000-07:00",
"airs" : {
"day" : null,
"time" : "18:00",
"timezone" : "Europe/London"
},
"runtime" : 28,
"certification" : null,
"network" : "BBC One",
"country" : "gb",
"trailer" : null,
"homepage" : null,
"status" : "ended",
"rating" : 0.0,
"votes" : 0,
"updated_at" : "2015-01-05T09:50:58.000Z",
"language" : "en",
"available_translations" : ["en"],
"genres" : [],
"aired_episodes" : 137
}
}, {
"airs_at" : "2015-01-01T10:30:00.000-08:00",
"episode" : {
"season" : 2015,
"number" : 1,
"title" : null,
"ids" : {
"trakt" : 1036730,
"tvdb" : 5069557,
"imdb" : null,
"tmdb" : null,
"tvrage" : null
},
"number_abs" : null,
"overview" : null,
"first_aired" : "2015-01-01T10:30:00.000-08:00",
"rating" : 0.0,
"votes" : 0,
"updated_at" : "2014-12-19T00:41:12.000Z",
"available_translations" : []
},
"show" : {
"title" : "Terra X",
"year" : 1982,
"ids" : {
"trakt" : 61149,
"slug" : "terra-x",
"tvdb" : 126301,
"imdb" : "tt0382491",
"tmdb" : null,
"tvrage" : null
},
"overview" : null,
"first_aired" : "1982-01-16T16:00:00.000-08:00",
"airs" : {
"day" : "Sunday",
"time" : "19:30",
"timezone" : "Europe/Berlin"
},
"runtime" : 45,
"certification" : null,
"network" : "ZDF",
"country" : "de",
"trailer" : null,
"homepage" : null,
"status" : "returning series",
"rating" : 0.0,
"votes" : 0,
"updated_at" : "2015-01-17T10:51:48.000Z",
"language" : null,
"available_translations" : [],
"genres" : ["documentary"],
"aired_episodes" : 635
}
}
]
}
当您在 JSON 上调用 JsonConvert.DeserializeObject(Of Object)
时,您实际返回的是 JObject
。 JObject
有一个 Properties()
方法,您可以使用该方法在不知道名称的情况下遍历其属性。您可以使用它来解决您的问题。
试试这个:
Dim dynamicObj = Newtonsoft.Json.JsonConvert.DeserializeObject(Of JObject)(res)
For Each prop As JProperty In dynamicObj.Properties()
For Each subObj As JObject In prop.Value
Console.WriteLine(subObj("show")("title"))
Next
Next
如果只有一个属性或者你只对第一个感兴趣,你可以使用System.Linq.First()
扩展方法来获取它并去掉外循环:
Dim dynamicObj = Newtonsoft.Json.JsonConvert.DeserializeObject(Of JObject)(res)
Dim premiers = dynamicObj.Properties().First().Value
For Each subObj As JObject In premiers
Console.WriteLine(subObj("show")("title"))
Next
Fiddle 这里:https://dotnetfiddle.net/lsieIU
我正在尝试解析下面的这个 JSON 字符串,这样我就可以循环浏览来自 trakt 电视网站的每个节目和剧集,而不管日期如何。 json 字符串是实际的样本,只有一个日期 2015-01-01,但实际流将有更多日期。 所以我的问题是,在下面的 vbcode 中,我如何在不指定日期的情况下循环播放电视节目?
希望有人能提供帮助。 , 提前致谢!
VB:
Dim Reader As New StreamReader(client.OpenRead("http://api.trakt.tv/calendar/premieres.json/5708fdcd69c6c666f4cbcf369cb22584/" & Now.Year & Now.AddMonths(-1).ToString("MM") & "01/100"))
Dim dynamicObj = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Object)(res)
Dim premiers = dynamicObj("2015-01-01")
For Each subObj As Object In premiers
Console.WriteLine(subObj("show")("title"))
Next
JSON:
{
"2015-01-01" : [{
"airs_at" : "2015-01-01T00:00:00.000-08:00",
"episode" : {
"season" : 2015,
"number" : 1,
"title" : "01/01/2015",
"ids" : {
"trakt" : 1685096,
"tvdb" : 5090491,
"imdb" : null,
"tmdb" : null,
"tvrage" : null
},
"number_abs" : null,
"overview" : "It's the Red Eye 2015 Mildly Amusing New Years special with guests Mike Baker, Bonnie McFarlane and a special treat...a retrospective of Frightened Correspondent Joe Machi!",
"first_aired" : "2015-01-01T00:00:00.000-08:00",
"rating" : 8.0,
"votes" : 1,
"updated_at" : "2015-01-18T21:10:22.000Z",
"available_translations" : []
},
"show" : {
"title" : "Red Eye w/Greg Gutfeld",
"year" : 2007,
"ids" : {
"trakt" : 3612,
"slug" : "red-eye-w-greg-gutfeld",
"tvdb" : 268484,
"imdb" : "tt0964948",
"tmdb" : 3635,
"tvrage" : null
},
"overview" : "Red Eye w/Greg Gutfeld is an American late-night/early-morning satirical talk show on the Fox News Channel, airing at 3:00 am ET Tuesday through Saturday and 11:00 pm Saturday and 2:00 am Sunday. The show features panelists and guests discussing the latest news in politics, pop culture, entertainment, business, sports, and religion. The show is hosted by Greg Gutfeld, a self-described libertarian, who is a former Maxim UK editor.",
"first_aired" : "2007-02-06T00:00:00.000-08:00",
"airs" : {
"day" : "Thursday",
"time" : "03:00",
"timezone" : "America/New_York"
},
"runtime" : 60,
"certification" : "TV-14",
"network" : null,
"country" : "us",
"trailer" : null,
"homepage" : null,
"status" : "ended",
"rating" : 0.0,
"votes" : 0,
"updated_at" : "2015-01-29T10:44:57.000Z",
"language" : "en",
"available_translations" : ["en"],
"genres" : ["comedy"],
"aired_episodes" : 322
}
}, {
"airs_at" : "2015-01-01T10:00:00.000-08:00",
"episode" : {
"season" : 2015,
"number" : 1,
"title" : "01/01/2015",
"ids" : {
"trakt" : 1602414,
"tvdb" : 5087659,
"imdb" : null,
"tmdb" : null,
"tvrage" : null
},
"number_abs" : null,
"overview" : null,
"first_aired" : "2015-01-01T10:00:00.000-08:00",
"rating" : 0.0,
"votes" : 0,
"updated_at" : "2015-01-05T09:50:48.000Z",
"available_translations" : []
},
"show" : {
"title" : "BBC News at Six",
"year" : null,
"ids" : {
"trakt" : 3499,
"slug" : "bbc-news-at-six",
"tvdb" : 264761,
"imdb" : "",
"tmdb" : 3521,
"tvrage" : null
},
"overview" : "The BBC News at Six is the evening news programme broadcast each night on British television channel BBC One and the BBC News channel at 18:00.",
"first_aired" : "1984-09-02T17:00:00.000-07:00",
"airs" : {
"day" : null,
"time" : "18:00",
"timezone" : "Europe/London"
},
"runtime" : 28,
"certification" : null,
"network" : "BBC One",
"country" : "gb",
"trailer" : null,
"homepage" : null,
"status" : "ended",
"rating" : 0.0,
"votes" : 0,
"updated_at" : "2015-01-05T09:50:58.000Z",
"language" : "en",
"available_translations" : ["en"],
"genres" : [],
"aired_episodes" : 137
}
}, {
"airs_at" : "2015-01-01T10:30:00.000-08:00",
"episode" : {
"season" : 2015,
"number" : 1,
"title" : null,
"ids" : {
"trakt" : 1036730,
"tvdb" : 5069557,
"imdb" : null,
"tmdb" : null,
"tvrage" : null
},
"number_abs" : null,
"overview" : null,
"first_aired" : "2015-01-01T10:30:00.000-08:00",
"rating" : 0.0,
"votes" : 0,
"updated_at" : "2014-12-19T00:41:12.000Z",
"available_translations" : []
},
"show" : {
"title" : "Terra X",
"year" : 1982,
"ids" : {
"trakt" : 61149,
"slug" : "terra-x",
"tvdb" : 126301,
"imdb" : "tt0382491",
"tmdb" : null,
"tvrage" : null
},
"overview" : null,
"first_aired" : "1982-01-16T16:00:00.000-08:00",
"airs" : {
"day" : "Sunday",
"time" : "19:30",
"timezone" : "Europe/Berlin"
},
"runtime" : 45,
"certification" : null,
"network" : "ZDF",
"country" : "de",
"trailer" : null,
"homepage" : null,
"status" : "returning series",
"rating" : 0.0,
"votes" : 0,
"updated_at" : "2015-01-17T10:51:48.000Z",
"language" : null,
"available_translations" : [],
"genres" : ["documentary"],
"aired_episodes" : 635
}
}
]
}
当您在 JSON 上调用 JsonConvert.DeserializeObject(Of Object)
时,您实际返回的是 JObject
。 JObject
有一个 Properties()
方法,您可以使用该方法在不知道名称的情况下遍历其属性。您可以使用它来解决您的问题。
试试这个:
Dim dynamicObj = Newtonsoft.Json.JsonConvert.DeserializeObject(Of JObject)(res)
For Each prop As JProperty In dynamicObj.Properties()
For Each subObj As JObject In prop.Value
Console.WriteLine(subObj("show")("title"))
Next
Next
如果只有一个属性或者你只对第一个感兴趣,你可以使用System.Linq.First()
扩展方法来获取它并去掉外循环:
Dim dynamicObj = Newtonsoft.Json.JsonConvert.DeserializeObject(Of JObject)(res)
Dim premiers = dynamicObj.Properties().First().Value
For Each subObj As JObject In premiers
Console.WriteLine(subObj("show")("title"))
Next
Fiddle 这里:https://dotnetfiddle.net/lsieIU