0

I'm trying to use VB.NET to access some deserialized data using newtonsoft's JSON library. So far I've gotten the JSON data deserialized and the data appears to be valid in the debugger, I just have no clue how to access it.

Basically, what I'm trying to do is I'm accessing twitch.tv's API to fetch a list of games, maybe into an array, and then get those games into a combo box.

This code is the result of a lot of googling and a c# to VB converter for the deserialization classes. Can anybody help me figure out what I need to do to access the names of the games in this chunk of data?

Imports Newtonsoft.Json
Imports System.IO

Public Class search

Private Sub search_Load(s    Ender As Object, e As EventArgs) Handles MyBase.Load
    getTwitchGames()

End Sub


Private Function getTwitchGames() As Array
    Dim strQueryURL As String = "https://api.twitch.tv/kraken/games/top"
    'create web request object and s    End the request
    Dim webclient As New System.Net.WebClient
    Dim json As String = webclient.DownloadString(strQueryURL)
    'Parse the result
    Dim test As RootObject = JsonConvert.DeserializeObject(Of RootObject)(json)
End Function

End Class
Public Class Links
Public Property self() As String
    Get
        Return m_self
    End Get
    Set(value As String)
        m_self = Value
    End Set
End Property
Private m_self As String
Public Property [next]() As String
    Get
        Return m_next
    End Get
    Set(value As String)
        m_next = Value
    End Set
End Property
Private m_next As String
End Class

Public Class Box
Public Property template() As String
    Get
        Return m_template
    End Get
    Set(value As String)
        m_template = Value
    End Set
End Property
Private m_template As String
Public Property small() As String
    Get
        Return m_small
    End Get
    Set(value As String)
        m_small = Value
    End Set
End Property
Private m_small As String
Public Property medium() As String
    Get
        Return m_medium
    End Get
    Set(value As String)
        m_medium = Value
    End Set
End Property
Private m_medium As String
Public Property large() As String
    Get
        Return m_large
    End Get
    Set(value As String)
        m_large = Value
    End Set
End Property
Private m_large As String
End Class

Public Class Logo
Public Property template() As String
    Get
        Return m_template
    End Get
    Set(value As String)
        m_template = Value
    End Set
End Property
Private m_template As String
Public Property small() As String
    Get
        Return m_small
    End Get
    Set(value As String)
        m_small = Value
    End Set
End Property
Private m_small As String
Public Property medium() As String
    Get
        Return m_medium
    End Get
    Set(value As String)
        m_medium = Value
    End Set
End Property
Private m_medium As String
Public Property large() As String
    Get
        Return m_large
    End Get
    Set(value As String)
        m_large = Value
    End Set
End Property
Private m_large As String
End Class

Public Class Links2
End Class

Public Class Game
Public Property name() As String
    Get
        Return m_name
    End Get
    Set(value As String)
        m_name = Value
    End Set
End Property
Private m_name As String
Public Property _id() As Integer
    Get
        Return m__id
    End Get
    Set(value As Integer)
        m__id = Value
    End Set
End Property
Private m__id As Integer
Public Property giantbomb_id() As Integer
    Get
        Return m_giantbomb_id
    End Get
    Set(value As Integer)
        m_giantbomb_id = Value
    End Set
End Property
Private m_giantbomb_id As Integer
Public Property box() As Box
    Get
        Return m_box
    End Get
    Set(value As Box)
        m_box = Value
    End Set
End Property
Private m_box As Box
Public Property logo() As Logo
    Get
        Return m_logo
    End Get
    Set(value As Logo)
        m_logo = Value
    End Set
End Property
Private m_logo As Logo
Public Property _links() As Links2
    Get
        Return m__links
    End Get
    Set(value As Links2)
        m__links = Value
    End Set
End Property
Private m__links As Links2
End Class

Public Class Top
Public Property viewers() As Integer
    Get
        Return m_viewers
    End Get
    Set(value As Integer)
        m_viewers = Value
    End Set
End Property
Private m_viewers As Integer
Public Property channels() As Integer
    Get
        Return m_channels
    End Get
    Set(value As Integer)
        m_channels = Value
    End Set
End Property
Private m_channels As Integer
Public Property game() As Game
    Get
        Return m_game
    End Get
    Set(value As Game)
        m_game = Value
    End Set
End Property
Private m_game As Game
End Class

Public Class RootObject
Public Property _total() As Integer
    Get
        Return m__total
    End Get
    Set(value As Integer)
        m__total = Value
    End Set
End Property
Private m__total As Integer
Public Property _links() As Links
    Get
        Return m__links
    End Get
    Set(value As Links)
        m__links = Value
    End Set
End Property
Private m__links As Links
Public Property top() As List(Of Top)
    Get
        Return m_top
    End Get
    Set(value As List(Of Top))
        m_top = Value
    End Set
End Property
Private m_top As List(Of Top)
End Class

2 Answers 2

1

You can try using LINQ to get game names into array, for example :

......
......
Dim test As RootObject = JsonConvert.DeserializeObject(Of RootObject)(json)
Dim result = test.top.Select(Function(x) x.game.name).ToArray()
Sign up to request clarification or add additional context in comments.

3 Comments

Wow! That one line of code was EXACTLY what worked! Perfectly formatted and everything! I would like to learn exactly how this line of code works though because I'm going to have to do something similar to this later on in my code. Can you explain what that "Dim result" line does a bit more?
the last line means : from each top select only game.name property then convert/project the result to array. Read up about LINQ in MSDN : Introduction to LINQ in Visual Basic
Yes, LINQ is absolutely magical! I get really bummed when I occasionally find a collection object that I can't use it on!
0

First you need attributes for the properties to get a JSON values:

Public Class RootObject
    <JsonProperty("total")> _
    Public Property Total As Int32

    'Add other properties in another classes too

End Class

Then get names using LINQ

Dim root As RootObject = JsonConvert.DeserializeObject(Of RootObject)(json)
Dim gameNames As List(Of String) = root.top.Select(Of String)(Function(tp) tp.game.name).ToList()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.