使用JavaScriptSerializer

时间:2015-08-14 06:04:34

标签: javascript json vb.net bootstrapper javascriptserializer

我正在为我的应用程序编写引导程序。它会下载最新版本的应用程序并进行安装。我的应用程序的JSON解析部分有问题。我试图从这个JSON获取信息:

{
    "Launcher": {
        "latestver": "<the latest version of the launcher>",
        "url": "<url to the launcher pack>",
    }
}

为了解析它,我创建了一个结构:

Public Structure Launcher

    Public latestver As String
    Public url As String

End Structure

然后我尝试从互联网上的JSON获取值:

    Dim client As New WebClient() With {.Proxy = Nothing}
    Dim Json As String = Await client.DownloadStringTaskAsync("<url to the launcher.json>")

    Dim jss As New JavaScriptSerializer()
    Dim thisLauncher As Launcher = jss.Deserialize(Of Launcher)(Json)

    RemoteLauncherVer = thisLauncher.latestver
    LauncherPackUrl = thisLauncher.url

但是当我使用MsgBox检查值时,值为Nothing

    MsgBox(RemoteLauncherVer)
    MsgBox(LauncherPackUrl)

有人可以帮我找出原因吗?

1 个答案:

答案 0 :(得分:0)

您在根级错过了一个班级。尝试制作这样的课程:

Public Class RootObject
    Public Property Launcher As Launcher
End Class

Public Class Launcher
    Public Property latestver As String
    Public Property url As String
End Class

然后像这样反序列化:

Dim jss As New JavaScriptSerializer()
Dim thisLauncher As Launcher = jss.Deserialize(Of RootObject)(json).Launcher