在.NET中反序列化JSON字符串

在.NET中反序列化JSON字符串,json,vb.net,Json,Vb.net,我知道在stackoverflow上有很多关于它的问题,但是我无法让它工作 我有一个JSON字符串() 我试图创建一个(简单但未完成的)模型来表示JSON数据,只是为了调试它: Public Class games Public Property gameList() As String() Public Property gameId() As String End Class 我真的不知道如何制作完整的模型,对我来说似乎很复杂 主代码: 1| Dim jss As New

我知道在stackoverflow上有很多关于它的问题,但是我无法让它工作

我有一个JSON字符串()

我试图创建一个(简单但未完成的)模型来表示JSON数据,只是为了调试它:

 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) 
gameData
是JSON字符串

我在第二行得到一个异常(
2 |
):
数组反序列化不支持类型“System.String”。

编辑:

我试过另一种方法

 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
公共类游戏
公共属性gameId,只要
公共属性mapId为整数
作为字符串的公共属性gameMode
作为字符串的公共属性gameType
公共属性gameQueueConfigId为整数
作为字符串()的公共属性参与者
公共属性观察员()作为字符串()
公共属性platformId为字符串
公共属性bannedChampions()作为字符串()
公共财产游戏开始时间尽可能长
公共财产游戏长度尽可能长
末级
公共类包装器
公共游戏列表()作为游戏
末级
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
Dim webClient作为新System.Net.webClient
Dim gameData As String=webClient.DownloadString(
"https://euw1.api.riotgames.com/lol/spectator/v3/featured-games?api_key=")
Dim json As String=gameData
Dim gwrapper=JsonConvert.DeserializeObject(包装器的)(json)
Dim game=gwrapper.gameList
如果game.Length=1,则
MsgBox(游戏(0).游戏模式)
如果结束
端接头
我得到一个例外:
分析值时遇到意外字符:{.Path'gameList[0]。参与者,第1行,位置133。
我建议您使用

编辑:


找到了一个安瑟尔只是再找一点

    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
公共类游戏
公共属性gameId,只要
公共属性mapId为整数
作为字符串的公共属性gameMode
作为字符串的公共属性gameType
公共属性gameQueueConfigId为整数
公共财产参与者作为参与者()
公共财产观察员作为观察员
公共属性platformId为字符串
公共财产横幅被禁止
公共财产游戏开始时间尽可能长
公共财产游戏长度尽可能长
末级
公开课参与者
作为整数的公共属性teamId
公共属性拼写为整数
公共属性拼写为整数
公共属性championId为整数
公共属性profileIconId为整数
公共属性名称作为字符串
作为布尔的公共属性bot
末级
公开课观察员
公共属性encryptionKey作为字符串
末级
公共课被禁
公共属性championId()为整数
公共属性teamId()为整数
公共属性pickTurn()为整数
末级
公共类包装器
公共游戏列表()作为游戏
末级
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
Dim webClient作为新System.Net.webClient
Dim gameData As String=webClient.DownloadString(
"https://euw1.api.riotgames.com/lol/spectator/v3/featured-games?api_key=")
Dim json As String=gameData
Dim gwrapper=JsonConvert.DeserializeObject(包装器的)(json)
Dim game=gwrapper.gameList
MsgBox(游戏(0.mapId.ToString)
端接头

@PurTahan你确定吗?因为我想
反序列化下面的stringuse…那里有很多UTIL,例如:将json转换为vb.netclasses@Fabio这真的很有帮助!谢谢你的评论。这是一个简单的解决方案,但是我在创建JSON字符串的数据结构/模型时遇到了问题,可以吗你给我一些建议/提示/例子?你能把你的Json字符串放进去吗?你可以通过将每行代码缩进四(4)个空格来创建代码块(你也可以选择所有代码,然后按
CTRL+K
)。使用反勾`只用于
内联代码
(代码位于规则文本的当前行内),不适用于占用多行的代码。有关更多信息,请参阅.OP已在
JsonConvert.DeserializeObject(包装器的)(Json)
行中使用
Newtonsoft.Json
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
    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