C
C#7mo ago
Penthus

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.
No description
No description
15 Replies
Jimmacle
Jimmacle7mo ago
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
[
{
"id": 1,
"posts": 1,
"name": "some name"
},
{
"id": 2,
"posts": 4,
"name": "some other name"
}
]
[
{
"id": 1,
"posts": 1,
"name": "some name"
},
{
"id": 2,
"posts": 4,
"name": "some other name"
}
]
Penthus
PenthusOP7mo ago
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.
[
"1",
"10",
"Bob"
],
[
"2",
"50",
"Ted"
],
[
"3",
"0",
"Karl"
],
[
"4",
"60",
"Mark"
],
[
"5",
"1",
"Barry"
]
]
[
"1",
"10",
"Bob"
],
[
"2",
"50",
"Ted"
],
[
"3",
"0",
"Karl"
],
[
"4",
"60",
"Mark"
],
[
"5",
"1",
"Barry"
]
]
Jimmacle
Jimmacle7mo ago
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>
Penthus
PenthusOP7mo ago
Alright thanks I will test it out now.
Jimmacle
Jimmacle7mo ago
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
Penthus
PenthusOP7mo ago
The JSON was a mix of a serialised string array and some manual tweaking of the text to try and get it to work.
string[,] leaderboardLongArray = new string[,]
{
{"1", "10", "Bob"},
{"2", "50", "Ted"},
{"3", "0", "Karl"},
{"4", "60", "Mark"},
{"5", "1", "Barry"}
};
string[,] leaderboardLongArray = new string[,]
{
{"1", "10", "Bob"},
{"2", "50", "Ted"},
{"3", "0", "Karl"},
{"4", "60", "Mark"},
{"5", "1", "Barry"}
};
Jimmacle
Jimmacle7mo ago
yeah that matches the json you showed, but you changed the "shape" of it so that json won't deserialize to the new shape
Penthus
PenthusOP7mo ago
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?
Jimmacle
Jimmacle7mo ago
why do you have the string array?
Penthus
PenthusOP7mo ago
This is all test data as the main goal is to be getting this data through a discord bot and then converting it.
Jimmacle
Jimmacle7mo ago
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
Penthus
PenthusOP7mo ago
[
{
"id": 1,
"posts": 10,
"name": "Bob"
}
],
[
{
"id": 2,
"posts": 50,
"name": "Ted"
}
],
[
{
"id": 3,
"posts": 0,
"name": "Karl"
}
],
[
{
"id": 4,
"posts": 60,
"name": "Mark"
}
],
[
{
"id": 5,
"posts": 1,
"name": "Barry"
}
]
]
[
{
"id": 1,
"posts": 10,
"name": "Bob"
}
],
[
{
"id": 2,
"posts": 50,
"name": "Ted"
}
],
[
{
"id": 3,
"posts": 0,
"name": "Karl"
}
],
[
{
"id": 4,
"posts": 60,
"name": "Mark"
}
],
[
{
"id": 5,
"posts": 1,
"name": "Barry"
}
]
]
I have changed it over from string[,] to a list of Leaderboard objects. Does this look like valid JSON?
SpReeD
SpReeD7mo ago
It is valid, try an online parser.
No description
Jimmacle
Jimmacle7mo ago
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 you
Penthus
PenthusOP7mo ago
Sorry 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 👍
Want results from more Discord servers?
Add your server