vb.net json.net解析结果

时间:2014-06-15 01:57:50

标签: asp.net vb.net parsing json.net

我想在在线视频流中获得大量观众。

此时此流已在线https://api.twitch.tv/kraken/streams/ludendi

作为参考,上述URL中的JSON如下所示:

{
    "_links": {
        "self": "https://api.twitch.tv/kraken/streams/ludendi",
        "channel": "https://api.twitch.tv/kraken/channels/ludendi"
    },
    "stream": {
        "_id": 9911305984,
        "game": "The Legend of Zelda: Majora's Mask",
        "viewers": 3848,
        "preview": {
            "small": "http://static-cdn.jtvnw.net/previews-ttv/live_user_ludendi-80x50.jpg",
            "medium": "http://static-cdn.jtvnw.net/previews-ttv/live_user_ludendi-320x200.jpg",
            "large": "http://static-cdn.jtvnw.net/previews-ttv/live_user_ludendi-640x400.jpg",
            "template": "http://static-cdn.jtvnw.net/previews-ttv/live_user_ludendi-{width}x{height}.jpg"
        },
        "_links": {
            "self": "https://api.twitch.tv/kraken/streams/ludendi"
        },
        "channel": {
            "mature": false,
            "abuse_reported": null,
            "status": "Team Ludendi presents speedrunning at DreamHack Summer 2014!",
            "display_name": "Ludendi",
            "game": "The Legend of Zelda: Majora's Mask",
            "delay": 0,
            "_id": 28673774,
            "name": "ludendi",
            "created_at": "2012-03-03T13:23:16Z",
            "updated_at": "2014-06-15T14:46:58Z",
            "logo": "http://static-cdn.jtvnw.net/jtv_user_pictures/ludendi-profile_image-e427022cc9f86446-300x300.png",
            "banner": "http://static-cdn.jtvnw.net/jtv_user_pictures/ludendi-channel_header_image-f33cb42afafa213c-640x125.jpeg",
            "video_banner": "http://static-cdn.jtvnw.net/jtv_user_pictures/ludendi-channel_offline_image-821fc3728572551f-640x360.jpeg",
            "background": null,
            "profile_banner": "http://static-cdn.jtvnw.net/jtv_user_pictures/ludendi-profile_banner-81f7a981e4b461a9-480.png",
            "profile_banner_background_color": null,
            "url": "http://www.twitch.tv/ludendi",
            "views": 5501318,
            "followers": 15749,
            "_links": {
                "self": "https://api.twitch.tv/kraken/channels/ludendi",
                "follows": "https://api.twitch.tv/kraken/channels/ludendi/follows",
                "commercial": "https://api.twitch.tv/kraken/channels/ludendi/commercial",
                "stream_key": "https://api.twitch.tv/kraken/channels/ludendi/stream_key",
                "chat": "https://api.twitch.tv/kraken/chat/ludendi",
                "features": "https://api.twitch.tv/kraken/channels/ludendi/features",
                "subscriptions": "https://api.twitch.tv/kraken/channels/ludendi/subscriptions",
                "editors": "https://api.twitch.tv/kraken/channels/ludendi/editors",
                "teams": "https://api.twitch.tv/kraken/channels/ludendi/teams",
                "videos": "https://api.twitch.tv/kraken/channels/ludendi/videos"
            }
        }
    }
}

我是初学者,所以我尝试修改this question的答案,但这对我不起作用。
以下是我到目前为止的情况:

Dim json As JObject = _
JObject.Parse(New WebClient().DownloadString("https://api.twitch.tv/kraken/streams"))

    If json IsNot Nothing AndAlso json.HasValues Then

        If json.SelectTokens("stream") IsNot Nothing _
           AndAlso json.SelectTokens("stream").Children().Any() Then

            Dim games() As JToken = json.SelectTokens("stream").Children().ToArray()

            For Each child As JToken In games

                MsgBox(child.Item("viewers"))
            Next
        End If

    End If
 End If

在Brian Rogers的帮助下,我尝试了这个:

    Dim json As String =
        New WebClient().DownloadString("https://api.twitch.tv/kraken/streams/ludendi")

    Dim root As JObject = JObject.Parse(json)

    Dim streams As JToken = root("stream")
    If streams IsNot Nothing Then

        For Each stream As JToken In streams.Children()

            Dim id As String = stream("_id").ToString()
            Dim game As String = stream("game").ToString()
            Dim viewers As String = stream("viewers").ToString()


            MsgBox(game)
            MsgBox(viewers)

        Next

    End If

但是我在这一行收到错误InvalidOperationException

            Dim id As String = stream("_id").ToString()

1 个答案:

答案 0 :(得分:0)

试试这个。这是获取特定流的游戏和查看者所需的最少代码。

Dim json As String = New WebClient() _
    .DownloadString("https://api.twitch.tv/kraken/streams/ludendi")

Dim root As JObject = JObject.Parse(json)

Dim stream As JToken = root("stream")

Dim game As String = stream("game").ToString()
Dim viewers As String = stream("viewers").ToString()

MsgBox(game & " (" & viewers & ")")

这将显示The Legend of Zelda: Majora's Mask (3609 viewers)