繁体   English   中英

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

[英]How to Parse a JSON string with dynamic date in vb.net?

我正在尝试解析以下JSON字符串,因此无论DATE如何,我都可以遍历trakt电视网站的每个节目和剧集。 json字符串是实际示例,并且只有一个日期2015年1月1日,但实际流将具有更多日期。 所以我的问题是,在下面的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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM