I am trying to read from a JSON file a series of items into an Object array. Unfortunately it comes back as null.
Very similar to this issue Unity C# JsonUtility is not serializing a list
So in my particular situation I have the class for the item:
[Serializable]
public class StateData
{
public string name;
public string location;
public string gameText;
}
The class of the collection of items:
[Serializable]
public class StateDataCollection
{
public List<StateData> stateDatas = new List<StateData>();
}
And the class where this is invoked:
public class JSONReader : MonoBehaviour
{
private const string Path = @"S:\repos\myProject\Assets\gameText\";
void Start()
{
string json = File.ReadAllText(Path + "StateData.json");
StateDataCollection stateDataCollection = new StateDataCollection();
stateDataCollection = JsonUtility.FromJson<StateDataCollection>(json);
}
}
Finally the json
{
"StateData":[
{
"name": "name",
"location":"location",
"gameText" : "gameText"},
{
"name": "name",
"location":"location",
"gameText" : "gameText"},
{
"name": "name",
"location":"location",
"gameText" : "gameText"
}]
}
The object comes back as null. The file is reading OK.
Any ideas where this is failing? TY!