从twitch.tv获取值JSON响应

时间:2015-10-01 05:24:46

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

我刚刚开始使用JSON.NET,我在从JSON项目中获取值时遇到了一些麻烦。

过去几天我遇到过一些代码,这些代码可以让我抓住名字而不是他的关联值。

我正在使用twitch.tv Web API。这是我的代码:

Dim sUrl As String = Convert.ToString("https://api.twitch.tv/kraken/streams/") & sUsername
Dim wRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(sUrl), HttpWebRequest)
wRequest.ContentType = "application/json"
wRequest.Accept = "application/vnd.twitchtv.v3+json"
wRequest.Method = "GET"

Dim wResponse As WebResponse = wRequest.GetResponse()
Dim dataStream As Stream = wResponse.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim res As String = reader.ReadToEnd()
Dim outer As JToken = JToken.Parse(res)
Dim inner As JObject = outer("stream").Value(Of JObject)()
Dim keys As List(Of String) = inner.Properties().[Select](Function(p) p.Name).ToList()

For Each k As String In keys
    Debug.WriteLine(k)
Next


reader.Close()
wResponse.Close()

原始JSON如下:

{
    "_links": {
        "self": "https: //api.twitch.tv/kraken/streams/jojosaysbreee",
        "channel": "https: //api.twitch.tv/kraken/channels/jojosaysbreee"
    },
    "stream": {
        "_id": 16717827552,
        "game": "TomClancy'sRainbowSix: Siege",
        "viewers": 15,
        "created_at": "2015-09-30T21: 19: 10Z",
        "video_height": 720,
        "average_fps": 59.9630366205,
        "is_playlist": false,
        "_links": {
            "self": "https: //api.twitch.tv/kraken/streams/jojosaysbreee"
        },
        "preview": {
            "small": "http: //static-cdn.jtvnw.net/previews-ttv/live_user_jojosaysbreee-80x45.jpg",
            "medium": "http: //static-cdn.jtvnw.net/previews-ttv/live_user_jojosaysbreee-320x180.jpg",
            "large": "http: //static-cdn.jtvnw.net/previews-ttv/live_user_jojosaysbreee-640x360.jpg",
            "template": "http: //static-cdn.jtvnw.net/previews-ttv/live_user_jojosaysbreee-{width}x{height}jpg"
        },
        "channel": {
            "_links": {
                "self": "http: //api.twitch.tv/kraken/channels/jojosaysbreee",
                "follows": "http: //api.twitch.tv/kraken/channels/jojosaysbreee/follows",
                "commercial": "http: //api.twitch.tv/kraken/channels/jojosaysbreee/commercial",
                "stream_key": "http: //api.twitch.tv/kraken/channels/jojosaysbreee/stream_key",
                "chat": "http: //api.twitch.tv/kraken/chat/jojosaysbreee",
                "features": "http: //api.twitch.tv/kraken/channels/jojosaysbreee/features",
                "subscriptions": "http: //api.twitch.tv/kraken/channels/jojosaysbreee/subscriptions",
                "editors": "http: //api.twitch.tv/kraken/channels/jojosaysbreee/editors",
                "videos": "http: //api.twitch.tv/kraken/channels/jojosaysbreee/videos",
                "teams": "http: //api.twitch.tv/kraken/channels/jojosaysbreee/teams"
            },
            "background": null,
            "banner": null,
            "broadcaster_language": "en",
            "display_name": "JOJOsaysbreee",
            "game": "TomClancy'sRainbowSix: Siege",
            "logo": "http: //static-cdn.jtvnw.net/jtv_user_pictures/jojosaysbreee-profile_image-26a326e1c867f257-300x300.jpeg",
            "mature": true,
            "status": "BetaHype<3",
            "partner": false,
            "url": "http: //www.twitch.tv/jojosaysbreee",
            "video_banner": "http: //static-cdn.jtvnw.net/jtv_user_pictures/jojosaysbreee-channel_offline_image-67b08d519585b45f-640x360.jpeg",
            "_id": 41382559,
            "name": "jojosaysbreee",
            "created_at": "2013-03-16T09: 33: 34Z",
            "updated_at": "2015-10-01T05: 15: 26Z",
            "delay": null,
            "followers": 2318,
            "profile_banner": "http: //static-cdn.jtvnw.net/jtv_user_pictures/jojosaysbreee-profile_banner-6abce6a882f4f9e4-480.jpeg",
            "profile_banner_background_color": "#ffffff",
            "views": 15939,
            "language": "en"
        }
    }
}

代码的响应是来自&#34; stream&#34;:

的所有名称
_id
game
viewers
created_at
video_height
average_fps
is_playlist
_links
preview
channel

我想要完成的是在每个项目之后获取所有相关值,但我似乎无法做到正确。我知道它需要更深入地迭代,但我已经尝试过IEnumerable方法并且在那里也没有成功。

非常感谢任何和所有帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用以下行将所有属性名称选择到列表中:

Dim keys As List(Of String) = inner.Properties().[Select](Function(p) p.Name).ToList()

您无法通过keys以这种方式获得除了属性名称之外的任何内容。相反,循环遍历Properties()集合并检查每个JProperty

For Each prop As JProperty In inner.Properties()
    Debug.WriteLine("{0} - {1}", prop.Name, prop.Value)
Next

此外,我建议进行一些更改。您用来提出请求的代码比应该更难:)

Dim sUrl As String = Convert.ToString("https://api.twitch.tv/kraken/streams/") & sUsername

无需致电Convert.ToString("") - 它已经是一个字符串。并且(尽管这是一个意见问题),我认为匈牙利语中的变量表示法并不是必需的。这更简单:

Dim url As String = "https://api.twitch.tv/kraken/streams/" & username

更容易使用WebClient

Dim client As New WebClient()
client.Headers.Add(HttpRequestHeader.Accept, "application/vnd.twitchtv.v3+json")
Dim responseJson = client.DownloadString(url)

完整示例:

Dim username As String = "???"
Dim url As String = "https://api.twitch.tv/kraken/streams/" & username

Dim client As New WebClient()
client.Headers.Add(HttpRequestHeader.Accept, "application/vnd.twitchtv.v3+json")
Dim responseJson As String = client.DownloadString(url)

Dim outer As JToken = JToken.Parse(responseJson)
Dim inner As JObject = outer("stream").Value(Of JObject)

For Each prop As JProperty In inner.Properties()
    Console.WriteLine($"{prop.Name} - {prop.Value}")
Next

轻松自在! :)