【问题标题】:vb.net json.net parse resultsvb.net json.net 解析结果
【发布时间】:2014-06-15 01:57:49
【问题描述】:

我想了解在线直播的观看人数。

目前此直播在线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()

【问题讨论】:

  • 请展示您的作品以便我们提供帮助。
  • 感谢您的帮助,问题已编辑

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


【解决方案1】:

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

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)

【讨论】:

  • 感谢您帮助我。但我想要的是获取特定流的信息。所以我需要使用特定的网址。这个现在在线(api.twitch.tv/kraken/streams/ludendi)当我尝试修改你的代码时,它不起作用
  • 我的代码在您更改 URL 时不起作用的原因是因为两个 URL 之间的 JSON 不同。以前的 URL https://api.twitch.tv/kraken/streams 返回包含流数组的 JSON,因此您需要一个循环来处理它们。单个流的 JSON 没有 streams 数组,因此流信息在 JSON 中更高一级,您不需要循环。我已经编辑了我的答案,以显示检索单个特定流的游戏和观众的最少代码。
  • 再次感谢您的帮助,现在可以正常使用了!
  • 没问题;很高兴我能帮上忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-05
  • 2014-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多