如何在vb.net中解析带有动态日期的JSON字符串?

时间:2015-01-29 21:44:59

标签: .net vb.net json.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
        }
    }
]
}

1 个答案:

答案 0 :(得分:2)

当您在JSON上致电JsonConvert.DeserializeObject(Of Object)时,您实际获得的是JObjectJObject有一个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

在这里小提琴:https://dotnetfiddle.net/lsieIU