I can't parse JSON in Unity. Here is the JSON file:
{
"play_classic": [
{ "english": "PLAY CLASSIC" },
{ "spanish": "JUEGO CLÁSICO" },
{ "portuguese": "JUEGO CLÁSICO" },
{ "russian": "IGRAT' V KLASSICHESKUYU" }
]
}
the structure and code looks like this:
[System.Serializable]
class Root
{
public List<PlayClassicData> play_classic { get; set; }
}
[System.Serializable]
class PlayClassicData
{
public string english { get; set; }
public string spanish { get; set; }
public string portuguese { get; set; }
public string russian { get; set; }
}
the implementation looks like this:
ParseJson
class:
public void Deserialize()
{
string path = Application.dataPath + "path...";
using (StreamReader r = new StreamReader(path))
{
string json = r.ReadToEnd();
Root classic = JsonUtility.FromJson<Root>(json);
//Root classic = JsonConvert.DeserializeObject<Root>(json);
Debug.Log("english:" + classic.play_classic[0].english);
Debug.Log("spanish:" + classic.play_classic[0].spanish);
}
}
The code throws an error:
NullReferenceException: Object reference not set to an instance of an object
Assets.Scripts.Infastructure.PARSER.ParseJson.Deserialize () (at Assets/Scripts/Infastructure/PARSER/ParseJson.cs:23)