ArgumentException: JSON must represent an object type.

Hi
I am struggling to understand why i am getting this error and hoping someone can spot the problem:

public void PrintUserTips()
{
string json = File.ReadAllText(Application.persistentDataPath + "/usertips.json");
Debug.Log("json for tips: " + json);
//**** error is happening here
PlayerTipsJSON loadedPlayerData = JsonUtility.FromJson<PlayerTipsJSON>(json);
int toppick = loadedPlayerData.data.Count;

for (int i = 0; i < loadedPlayerData.data.Count; i++)
{

Debug.Log(loadedPlayerData.data[i].ContentTitle + "\n");
Debug.Log(loadedPlayerData.data[i].ContentBody + "\n");

}

ContentBody.text = loadedPlayerData.data[RandomiserTip(toppick)].ContentBody;
ContentBody1.text = loadedPlayerData.data[RandomiserTip(toppick)].ContentBody;
ContentBody2.text = loadedPlayerData.data[RandomiserTip(toppick)].ContentBody;
}
[System.Serializable]
public class PlayerData
{
// public string fromJSONusername;
public string ContentTitle;
public string ContentBody;

}

[System.Serializable]
public class PlayerTipsJSON
{
public List<PlayerData> data;
}

here is the JSON file

[{"ContentTitle":"test data minimum value","ContentBody":"this is a test message that should display when the user value is greater than the minimum value"},{"ContentTitle":"test data maximum value","ContentBody":"this is a test message that should display when the user value is less than the maximym value"},{"ContentTitle":"test data boolean true","ContentBody":"This is a test message for content where boolean is true"}]

thanks

The error means your json file must contain object, but it contains array. Unity json do not supports root level array in the json documents.

1 Like

thanks - yes that was the problem.
Working now as i added {“data”:[…]}

1 Like