C
C#2y ago
JustiPhi

Issue with JSON Serialisation

When converting from object to json i need my list of object to be as follows "group": [{ "item": {}, "item": {} }] Instead I am getting "group": [{ "item": [ {}, {} ] }] How would I create my object to serialise it in the correct way? Thanks in advance
10 Replies
JustiPhi
JustiPhi2y ago
currently the group class looks like this
public class FilterGroup
{
public List<FilterItem> item { get; set; }
}
public class FilterGroup
{
public List<FilterItem> item { get; set; }
}
Jayy
Jayy2y ago
I mean... you declared item as a list of objects lol So it serialized to a json list
JustiPhi
JustiPhi2y ago
so i cant really make it serialise the correct way? without making a custom toJson method
Jayy
Jayy2y ago
Err, i mean if you don't want it to be a list don't make it a list It's doing it the correct way right now
JustiPhi
JustiPhi2y ago
but then how do i allow multiple
Jayy
Jayy2y ago
I mean... u use a list lol like ur doing Which will generate a json with a list This first example only allows one item So it doesn't allow multiple
JustiPhi
JustiPhi2y ago
which means i need a custom ToJson method for it i guess
Jayy
Jayy2y ago
No... You don't This is a weird question cuz you want a list but you want it to not be a list? Like that makes 0 sense
JustiPhi
JustiPhi2y ago
i may have been misreading the json havent been working with json in years yeah im stupid and i got it, thanks
Jayy
Jayy2y ago
👍