Ben
Ben

Reputation: 306

Deserializing Json VB.NET

I am trying to deserialize data from a webserver for a game launcher.
The API docs provide me the data in json format, but I have been struggling to be able to read that data I am given and store it as a variable to use to log in a player. I have also tried a few other things but I am just stuck now.

Normally a response from the server would like like this:

{"success":"true","gameserver":"gameserver-alpha.toontownrewritten.com","cookie":"deadbeefdeafbeef0x123"}

The code to deserialize the data:

Dim result() As TTRServerResponse = JsonConvert.DeserializeObject(Of TTRServerResponse())(responseFromServer)

Class with the variables I want to store

Public Class TTRServerResponse
    Public Property success As String
    Public Property eta As String
    Public Property position As String
    Public Property queueToken As String
    Public Property cookie As String
End Class

Any ideas where I messed up or what I should do? Thanks, Ben.

EDIT: Finally figured it out, I needed to change my result to:

Dim result As TTRServerResponse = JsonConvert.DeserializeObject(Of TTRServerResponse) (responseFromServer)

I also was returning the data incorrectly to my other sub by returning just result. What I needed to do was return result.success. I now have a better understanding.

Upvotes: 2

Views: 1770

Answers (1)

Nkosi
Nkosi

Reputation: 247098

You are trying to deserialize the JSON into an array but in your example it is a single object.

So assuming

Dim responseFromServer As String

is equal to

{"success":"true","gameserver":"gameserver-alpha.toontownrewritten.com","cookie":"deadbeefdeafbeef0x123"}

Which, according to the documentation you linked to, is suppose to be a single object,

then you need to update your code to deserialize a single object as appose to an array

Dim result As TTRServerResponse = JsonConvert.DeserializeObject(Of TTRServerResponse)(responseFromServer)

Upvotes: 1

Related Questions