JSON Deserialize
Hi all, have been researching this for a couple of hours now but not quite understanding it.
I have a leaderboard I am wanting to load/save and thought JSON would be the easiest way but am struggling with getting the JSON formed how I want it.
I have tried a few different ways but keep running into the same issue, which I believe is that it's getting an array of my class.
I have tried changing it to a List<t> I am trying to get but still the same error message.
15 Replies
your json doesn't look like it's an array
arrays are enclosed in
[
and ]
hard to read the string in the screenshot
actually i have no idea what this json is, it doesn't match your structure at all
you have an object with a bunch of duplicate user properties and each user is represented as an array of objects
where did the json come from?
it should look like
I have been messing around with the JSON file as originally I had it like this, but changed it due to discovering I can't have everything on the same layer and added the User layer.
yeah that json is wrong, it's an array of string arrays but your C# model is an array of Leaderboard objects
see what i sent
that will deserialize to List<Leaderboard>
Alright thanks I will test it out now.
if you serialize the data to json in your code you are guaranteed to be able to deserialize it
handwriting it is just error prone
The JSON was a mix of a serialised string array and some manual tweaking of the text to try and get it to work.
yeah that matches the json you showed, but you changed the "shape" of it so that json won't deserialize to the new shape
I was trying to convert that into something I could serialise.
Yeah, how would you go about coverting a string array into my object to then serialise for JSON?
why do you have the string array?
This is all test data as the main goal is to be getting this data through a discord bot and then converting it.
if you want the data to be a list of Leaderboard objects create it like that
no point in creating data in a different format just to have to convert it
I have changed it over from string[,] to a list of Leaderboard objects. Does this look like valid JSON?
It is valid, try an online parser.
![No description](https://cdn.answeroverflow.com/1238504630040789072/image.png)
that is not valid if your C# model is List<Leaderboard>
that is a List<List<Leaderboard>>
if you just take a
List<Leaderboard>
in your code and serialize it to JSON it will be correct
not sure why you're writing this by hand, let C# do it the right way for youSorry that was serialised but I accidentally still had it as a multidimential array which is why it had the extra layer.
All working now, thank you for your help 👍