我正在尝试使用VB.NET来使用newtonsoft的JSON库访问一些反序列化的数据。到目前为止,我已经对JSON数据进行了反序列化,并且数据在调试器中似乎是有效的,我只是不知道如何访问它。
基本上,我正在尝试做的是访问twitch.tv的API来获取游戏列表,可能是一个数组,然后将这些游戏放入组合框中。
这段代码是大量google搜索的结果,也是反序列化类的c#到VB转换器的结果。任何人都可以帮我弄清楚我需要做些什么来访问这一块数据中的游戏名称?
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
答案 0 :(得分:1)
您可以尝试使用LINQ将游戏名称转换为数组,例如:
......
......
Dim test As RootObject = JsonConvert.DeserializeObject(Of RootObject)(json)
Dim result = test.top.Select(Function(x) x.game.name).ToArray()
答案 1 :(得分:0)
首先,您需要属性的属性才能获得JSON
值:
Public Class RootObject
<JsonProperty("total")> _
Public Property Total As Int32
'Add other properties in another classes too
End Class
然后使用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()