This question already has an answer here:
I have been trying for a couple hours to deserialize info from my server for our high score system. However I am getting an error:
ArgumentException: JSON must represent an object type.
This is how I am deserializing (scores
is of type Scores):
public void Deserialize()
{
Debug.Log("From Server is ::::: -----> " + FromServer);
scores = JsonUtility.FromJson<Scores> (FromServer);
}
This is my Json info which is generated by my server. 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 these are the classes I'm trying to serialize into:
[System.Serializable]
public class LeaderBoardEntries
{
public int ScoreID;
public string GoogleID;
public int Score;
}
[System.Serializable]
public class Scores
{
public List<LeaderBoardEntries> scores;
}
You can only have 1 object in the .json file to be able to deserialize it via
JsonUtility
. Your .json is not an object, it's an array of objects. It should be in this format:this .json string can be deserialized into a
Scores
object.I agree Unity should make their documentation a little more clear. They need to emphasize that you can deserialize .json files representing only 1 object.