通过Twitch Json迭代

时间:2015-06-17 15:37:46

标签: json vb.net iteration twitch

我还不太擅长这个,不确定我的主题是否准确描述了我的需要。我知道它可能已被回答,我很难理解答案,因为它们不能直接应用于我的数据。我试图找出如何遍历此URL中的数据。

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

这就是我一直在使用的。

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

我想我在收集收藏中的观众时遇到了麻烦,以便我可以遍历它们,有人能指出我在这方向的正确方向吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

“Chatters”实际上是根对象中的Type。如果你要创建类,它们将如下所示:

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

查看者是Chatters属性中的数组(Root.Chatters.Viewers)。没有班级:

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

输出:

  

04paynem
  0morningstar0
  0rchlann
  0riginus
  10108abc

如果要对类进行反序列化:

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