簡體   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