3

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;
}
code11
  • 1,986
  • 5
  • 29
  • 37
John Esslemont
  • 87
  • 1
  • 3
  • 10

1 Answers1

7

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:

{
    scores: [
    {
        "ScoreID": "8",
        "GoogleID": "asasda",
        "Score": "258"
    },
    {
        "ScoreID": "11",
        "GoogleID": "kjjahushcjkasjkcajskndas",
        "Score": "258"
    }
    ]
}

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.

Nika Kasradze
  • 2,834
  • 3
  • 25
  • 48
  • Thanks for that but it doesnt seem to work, scores = JsonUtility.FromJson ("{\"scores\": " + FromServer + "}"); I added the beginning string to this as my server does not of course out put this, i no longer get any errors but i also do not get my class filled with info from the json – John Esslemont Apr 05 '17 at 16:03
  • Got it working was a simple typo. Thank you! – John Esslemont Apr 05 '17 at 17:25