Search code examples
jsonvb.netparsingjson.nettwitch

Getting values from twitch.tv JSON response


I have just started using JSON.NET and I am having some trouble getting the values from JSON items.

I have come across some code over the past few days which will allow me to grab the names but not he associated values.

I am working with the twitch.tv Web API. Here is my code:

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()

The raw JSON is as follows:

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

The response from the code is all the names from "stream":

_id
game
viewers
created_at
video_height
average_fps
is_playlist
_links
preview
channel

What I am trying to accomplish is getting all the associated values after each one of those items but I cannot seem to get it right. I know it needs to iterate deeper but I've tried IEnumerable method and was unsuccessful there also.

Any and all help is much appreciated.


Solution

  • You're selecting all of the property names into a list with this line:

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

    You won't be able to get anything other than the property names from keys this way. Instead, loop over the Properties() collection and examine each JProperty:

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

    In addition, I'd recommend a few changes. The code you're using to make the request is harder than it should be :)

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

    No need to call Convert.ToString("") - it's already a string. And (although it's a matter of opinion), I think Hungarian notation for the variables isn't necessary. This is much simpler:

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

    Much easier to just use WebClient:

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

    Complete example:

    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
    

    Easy peasy! :)