- Home /
Json deserialisation of server results?
Hi there,
I have been trying for a couple hours to deserialize info from my server for our high score system. Everytime i try to i will get n error "ArgumentException: JSON must represent an object type."
This is my Json info which is generated by my server when i echo it out. What am i doing wrong?
[
{
"ScoreID": "8",
"GoogleID": "asasda",
"Score": "258"
},
{
"ScoreID": "11",
"GoogleID": "kjjahushcjkasjkcajskndas",
"Score": "258"
},
{
"ScoreID": "10",
"GoogleID": "aakhskjbjkabsjjkasd",
"Score": "258"
},
{
"ScoreID": "9",
"GoogleID": "mnbabksgkajsd",
"Score": "258"
},
{
"ScoreID": "13",
"GoogleID": "kajiuskabkjshdkhausd",
"Score": "258"
},
{
"ScoreID": "7",
"GoogleID": "asasdas,mgkhgjhbhjaf",
"Score": "258"
},
{
"ScoreID": "6",
"GoogleID": "ss",
"Score": "258"
},
{
"ScoreID": "5",
"GoogleID": "kljkuasjkba",
"Score": "258"
},
{
"ScoreID": "4",
"GoogleID": "asdadasdasdasdw",
"Score": "258"
},
{
"ScoreID": "3",
"GoogleID": "asdadsasd",
"Score": "258"
},
{
"ScoreID": "2",
"GoogleID": "kbaskjkjsbnkjas",
"Score": "258"
},
{
"ScoreID": "12",
"GoogleID": "lahaushyuiahkjsjksd",
"Score": "258"
},
{
"ScoreID": "1",
"GoogleID": "254asdasd54a5s1das2d1as54d",
"Score": "259"
},
{
"ScoreID": "14",
"GoogleID": "kjaskjhjkahsjkdnjkasd",
"Score": "6859"
}
]
and this is the classes im trying to serialise into
[System.Serializable]
public class LeaderBoardEntries
{
public int ScoreID;
public string GoogleID;
public int Score;
}
[System.Serializable]
public class Scores
{
public List<LeaderBoardEntries> scores;
}
There is built in functionality for parsing json name "JsonUtility" which serialized/deserialized json string to object or vice versa.
string json = JsonUtility.ToJson(myObject);
Unity Official Documentation For JSONSerialization link JSONSerialization
Answer by elenzil · Apr 05, 2017 at 05:08 PM
"ArgumentException: JSON must represent an object type."
an object type in json is a {}. your json is a [], which is an array.
if you're able to change the server code, modify it to emit something like
{
"scores": [
{
"ScoreID": "1",
"GoogleID": "254asdasd54a5s1das2d1as54d",
"Score": "259"
},
{
"ScoreID": "12",
"GoogleID": "lahaushyuiahkjsjksd",
"Score": "258"
}
]
}
if you're not able to modify the server code, have your client modify the json string before parsing. eg
string newJSONString = "{ \"scores\": " + oldJSONString + " }";
.. i would strongly advise trying to to it on the server, tho.
also, unrelatedly, if it were me i would rename your "LeaderBoardEntries" class to "LeaderBoardEntry".
actually, the docs are pretty vague. arrays are not mentioned.
Your answer
Follow this Question
Related Questions
how to deserialize json with arbitrary string keys using JsonUtility(unity c#)? 4 Answers
Deserializing dictionary with 'Json.Net' for Unity returns a null object. 0 Answers
Hashtable as object literal serialization toJSON 0 Answers
Parsing "complex" json 1 Answer
Deserialize Facebook friends result 1 Answer