解码复杂的JSON阵列vb.net

时间:2013-09-29 02:17:08

标签: arrays json vb.net serialization decode

我已经遇到很多麻烦了几天了,我对此感到非常沮丧! 我想知道如何解码VB.NET 4.5 Framework中的JSON数组。我正在尝试使用deserialize / serialize类。 这是数组:

  

{ “LaunchIDs”:[ “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”], “FavoriteID”: null,“用户名”:“测试”,“错误”:0,“消息”:“您已成功登录”}

数组的代码是

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”。

1 个答案:

答案 0 :(得分: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)

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