Iterating through Twitch Json

201 views Asked by At

I'm not very good at this yet, not sure if my subject even accurately describes what I need. I know it's probably been answered, I'm having a hard time understanding the answers since they don't directly apply to my data. I am trying to figure out how I can iterate through the data in this URL.

http://tmi.twitch.tv/group/user/twitch/chatters

This is what I've been using.

Dim url = "http://tmi.twitch.tv/group/user/" & ConnectionInformation.Channel.TrimStart("#") & "/chatters"
Dim json As String = Nothing
Dim wc As New WebClient()
json = wc.DownloadString(url)

Dim root As JToken = JToken.Parse(json)
For Each item As JToken In root("chatters")
'I've tried several things here and I can't find a good way to iterate through the viewers found here.        
Next

I guess I'm having trouble getting the viewers in a collection so that I can iterate through them, can someone point me in the right direction here?

Thank you.

1

There are 1 answers

4
Ňɏssa Pøngjǣrdenlarp On BEST ANSWER

"Chatters" is actually a Type in the root object. If you were to create classes, they would look like this:

Public Class RootChatter
    Public Property _links As _Links
    Public Property chatter_count As Integer
    Public Property chatters As Chatters
End Class

Public Class _Links
End Class

Public Class Chatters
    Public Property moderators As String()
    Public Property staff As String()
    Public Property admins As String()
    Public Property global_mods As String()
    Public Property viewers As String()
End Class

Viewers is an array in the Chatters Property (Root.Chatters.Viewers). Without the class:

Dim root As JToken = JToken.Parse(jstr)
Dim chatters = root("chatters")("viewers")

For n As Integer = 0 To chatters.Count - 1
    Console.WriteLine(chatters(n))
Next

Output:

04paynem
0morningstar0
0rchlann
0riginus
10108abc

If you were to deserialize to the classes:

Dim jc = JsonConvert.DeserializeObject(Of RootChatter)(jstr)
Dim viewers = jc.chatters.viewers