vb.net json.net parse results

混江龙づ霸主 提交于 2019-12-11 18:58:12

问题


I would like to get the amount of viewers on an online stream.

Right now this stream is online https://api.twitch.tv/kraken/streams/ludendi

For reference, the JSON from the above URL looks like this:

{
    "_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"
            }
        }
    }
}

I'm a beginner, so I tried to modify the answer from this question, but it isn't working for me.
Here is what I have so far:

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

With the help of Brian Rogers i tried this:

    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

But i got an error InvalidOperationException on this line:

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

回答1:


Try this instead. This is the minimum code you need to get the game and viewers for a specific stream.

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 & ")")

This will display The Legend of Zelda: Majora's Mask (3609 viewers).



来源:https://stackoverflow.com/questions/24225918/vb-net-json-net-parse-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!