简体   繁体   中英

Deserializing a JSON object with JavaScriptSerializer

I'm writing a bootstrap for my application. It downloads the latest version of the app and installs it. I have a problem with the JSON parsing part of the app. I am trying to get the info from this JSON:

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

To parse it, I created a structure:

Public Structure Launcher

    Public latestver As String
    Public url As String

End Structure

Then I tried to get the values from the JSON on the internet:

    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

But when I checked the values with a MsgBox, the values were Nothing :

    MsgBox(RemoteLauncherVer)
    MsgBox(LauncherPackUrl)

Can someone help me figure out why?

You're missing a class at the root level. Try making your classes like this:

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

Then deserialize like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM