0

几天来,我一直在为一个问题烦恼,我对此感到非常沮丧!我想知道如何在 VB.NET 4.5 Framework 中解码 JSON 数组。我正在尝试使用反序列化/序列化类。这是数组:

{"LaunchIDs":["2","3","4","5","6","7","8","9","10"],"FavoriteID":null,"用户名":"Test","error":0,"message":"您已成功登录"}

数组的代码是

Public Class JSONCode
   Public LaunchIDs As Integer
   Public FavoriteIDAs Integer
   Public username As String
   Public [error] As Integer
   Public message As Integer
End Class

这是我在应用程序中使用它的方式。

 Dim client As New WebClient()
 Dim jsonResponse As String = "{""LaunchIDs"":[""2"",""3"",""4"",""5"",""6"",""7"",""8"",""9"",""10""],""FavoriteID"":null,""username"":""Test"",""error"":0,""message"":""You have successfully logged in""}")
 Dim serializer As New JavaScriptSerializer()
 Dim response As JSONCode = serializer.Deserialize(Of JSONCode)(jsonResponse)
        code = response.error
        If code = 0 & response.message = "You have successfully logged in" Then
            LoginFunction = True
            botids = response.botids
            ListBox1.AddItem(botids)
        Else
            Alert(Style.Critical, response.message)
        End If
 End If

该数组将从一个网址中检索(在我解决了这个问题之后),所以它现在有点乱。Alert() 只是一个小的自定义 msgBox 函数。

尝试调试时出现此错误。数组的反序列化不支持类型“System.Int32”。

4

1 回答 1

0

你的班级声明是错误的,应该是

Public Class JSONCode_cor
    Public LaunchIDs As Integer()
    Public FavoriteID As Integer()
    Public username As String
    Public [error] As Integer
    Public message As String
End Class

这是我在代码中使用的:

Dim jsonResponse As String = "{""LaunchIDs"":[""2"",""3"",""4"",""5"",""6"",""7"",""8"",""9"",""10""],""FavoriteID"":null,""username"":""Test"",""error"":0,""message"":""You have successfully logged in""}"
Dim serializer As New Web.Script.Serialization.JavaScriptSerializer()
Dim response As Object = serializer.Deserialize(Of JSONCode_cor)(jsonResponse)

它有效。您的问题是您将 LaunchIDs 声明为,Integer但实际上它是一个数组Integer()

于 2013-09-30T07:14:44.640 回答