我知道在 stackoverflow 上有很多关于它的问题,但是,我无法让它工作。
我有一个JSON字符串(Pastebin Link)。
我试图创建一个(简单而未完成的)模型来表示JSON数据,只是为了调试它:
MapService.ServiceToken = "MyKey";
我真的不知道怎么做完整的模型,对我来说似乎很复杂。
主要代码:
Public Class games
Public Property gameList() As String()
Public Property gameId() As String
End Class
1| Dim jss As New JavaScriptSerializer()
2| Dim model As games = jss.Deserialize(Of games)(gameData)
3| MsgBox(model.gameId(0).ToString)
是JSON字符串。
我在第二行(gameData
..)上得到例外:
2|
修改
我尝试了另一种方式
Type 'System.String' is not supported for deserialization of an array.
我得到例外:
Public Class games
Public Property gameId As Long
Public Property mapId As Integer
Public Property gameMode As String
Public Property gameType As String
Public Property gameQueueConfigId As Integer
Public Property participants As String()
Public Property observers() As String()
Public Property platformId As String
Public Property bannedChampions() As String()
Public Property gameStartTime As Long
Public Property gameLength As Long
End Class
Public Class wrapper
Public gameList() As games
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim webClient As New System.Net.WebClient
Dim gameData As String = webClient.DownloadString(
"https://euw1.api.riotgames.com/lol/spectator/v3/featured-games?api_key=<key>")
Dim json As String = gameData
Dim gwrapper = JsonConvert.DeserializeObject(Of wrapper)(json)
Dim game = gwrapper.gameList
If game.Length = 1 Then
MsgBox(game(0).gameMode)
End If
End Sub
答案 0 :(得分:1)
我建议您使用Newtonsoft.Json。
Dim x As String = Newtonsoft.Json.JsonConvert.SerializeObject(GameObj)
Dim obj As games = Newtonsoft.Json.JsonConvert.DeserializeObject(GameStr)
编辑:
Public Class myObj
Public gameList() As game
Public clientRefreshInterval As Integer
End Class
Public Class game
Public gameId As Integer
Public mapId As Integer
Public gameMode As String
Public gameType As String
Public gameQueueConfigId As Integer
Public participants() As participant
Public observers As observer
Public platformId As String
Public bannedChampions() As bannedChampion
Public gameStartTime As Integer
Public gameLength As Integer
End Class
Public Class participant
Public teamId As Integer
Public spell1Id As Integer
Public spell2Id As Integer
Public championId As Integer
Public profileIconId As Integer
Public summonerName As String
Public bot As Boolean
End Class
Public Class observer
Public encryptionKey As String
End Class
Public Class bannedChampion
Public championId As Integer
Public teamId As Integer
Public pickTurn As Integer
End Class
答案 1 :(得分:0)
只需要再搜索一下!
Public Class games
Public Property gameId As Long
Public Property mapId As Integer
Public Property gameMode As String
Public Property gameType As String
Public Property gameQueueConfigId As Integer
Public Property participants As participants()
Public Property observers As observers
Public Property platformId As String
Public Property bannedChampions As banned()
Public Property gameStartTime As Long
Public Property gameLength As Long
End Class
Public Class participants
Public Property teamId As Integer
Public Property spell1Id As Integer
Public Property spell2Id As Integer
Public Property championId As Integer
Public Property profileIconId As Integer
Public Property summonerName As String
Public Property bot As Boolean
End Class
Public Class observers
Public Property encryptionKey As String
End Class
Public Class banned
Public Property championId() As Integer
Public Property teamId() As Integer
Public Property pickTurn() As Integer
End Class
Public Class wrapper
Public gameList() As games
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim webClient As New System.Net.WebClient
Dim gameData As String = webClient.DownloadString(
"https://euw1.api.riotgames.com/lol/spectator/v3/featured-games?api_key=<key>")
Dim json As String = gameData
Dim gwrapper = JsonConvert.DeserializeObject(Of wrapper)(json)
Dim game = gwrapper.gameList
MsgBox(game(0).mapId.ToString)
End Sub