Reputation: 79
I am trying to parse this JSON string below so I can loop thru each show and episode from trakt tv site irrespective of the DATE. The json string is a sample from the actual and has only one date 2015-01-01 but the actual stream will have more dates. So my question is, in the below vbcode how do I loop thru the tv shows without specifying a date?
hope someone can help. , thanks in advance!
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
}
}
]
}
Upvotes: 0
Views: 1455
Reputation: 129787
When you call JsonConvert.DeserializeObject(Of Object)
on your JSON what you are actually getting back is a JObject
. JObject
has a Properties()
method which you can use to iterate through its properties without knowing the names. You can use this to get around your problem.
Try this:
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
If there's only one property or you're only interested in the first one of many, you can use the System.Linq.First()
extension method to get it and lose the outer loop:
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 here: https://dotnetfiddle.net/lsieIU
Upvotes: 2