JSON Parse

Hey there guys,

Quick question…How do you guys parse the json data in the payload? I have tried like 5 libraries and each of them is kind of tripping over the dynamic property name in the gameobjects.

I have also tried using the included models to deserialize against as well as my own but to no avail.

I am using c#

Thanks :slight_smile:

1 Like

@jhonnywar64, I studied the code that the Reference Bot is using to parse the JSON. The reference bot also using C#. That code should help you.

1 Like

Hi @jhonnywar64 , can you please review how the reference bot and Net Core starter do the same, and let me know if you don’t manage to figure it out?

Typically, with the Net Core client, you don’t have to do any manual serialisation / deserialisation. It is handled by the SignalR client package for you, with the type specified in your connection listener.

1 Like

Thanks for all the responses guys <3

I have managed to read the json using the newtonsoft libraries.

But instead of using the JsonConvert.DeserializeObject, which is the common example on the net, I used JArray.Parse. From there, like @James.Reynolds mentioned, there was a lot of manual serialization into the models provided.

Thanks again, but for now, its time to math #linqfordays

Hi @jhonnywar64 , Glad to hear you got it sorted.

Still quite interested as to why you’ve had to deserialize manually?

The signalR client library will deserialise for you, with the given type parameter:
connection.on<GameStateDto>(cbHandler)

The above will cause the signalR client library to serialise the payload into a GameStateDto object for you, and then pass that as a parameter to the cbHandler function.

Otherwise, serialising to a JArray and using JsonLinq isn’t a bad option either! #LinqIsLove #LinqIsLife am I right? :stuck_out_tongue:

Let me know if you need further assistance.

Also, there is a new starter pack version available - strongly suggest you update!

I had to deserialize manually for a custom visualizer I am building, within the bot though, I am using the GameStateDto.

At first, I tried to read the logfile into a string and then using newtonsoft, I tried to deserialize.

JsonConvert.DeserializeObject(json);
JsonConvert.DeserializeObject<List>(json)
etc.

I think what was tripping up some of the libraries when using a model to deserialize is the dynamic propery name (GUID) of the GameObjects.

Ah, yes. That makes a lot more sense.
You will in that case have to deserialise to a dictionary or , in netwonsoft’s case, there is JObject and JArray you can use. These also allow you to use linq which is nice!