Question by
copyado123 · May 25, 2017 at 04:29 PM ·
unity 5json
JSON to Object error Object reference not set to an instance of an object
So i have this string that callback function gives which contains all the json data
private string jsonResponse = @"{""players""[{""name"":""cc"",""rank"":29}, ...]}";
And when i try to convert it from json to object using JsonUtility.FromJson it throws
Object reference not set to an instance of an object
I've created object classes(root and normal class) and it still doesn't work, here is the code:
void actionTest(int i, string s)
{
Debug.Log(i + " " + s);
Rootobject rootObj = new Rootobject();
rootObj = JsonUtility.FromJson<Rootobject>(s);
Debug.Log(rootObj.players.Count);
}
[System.Serializable]
public class Rootobject
{
public List<Player> players { get; set; }
}
[System.Serializable]
public class Player
{
public string name { get; set; }
public int rank { get; set; }
}
This all happens when a button is pressed!
Comment
Answer by Sabre-Runner · Jul 10, 2017 at 01:01 PM
First of all, you are missing a colon between players and the rest of the array.
Secondly, to add a quotation mark in the string you use \"
Thirdly, that's not a List. That's an array.