I'm trying to read in a local JSON file that contains various objects. But the object always returns null.
I know that the Resources.Load<TextAsset>(_directory + _fileName).text; has successfully found the file, since I can output the text to the console.
My goal is to be able to ask for a key and get back the value for the selected language. ie: hello_world.sp would return Hola, mundo.
However, anytime I go to access any object like Debug.Log(lang.languageList.Count); I get the error:
NullReferenceException: Object reference not set to an instance of an object
Eventually, I would like to be able to add additional language values, fr,it, etc...
Can anyone see what I am doing wrong?
lang.json
{
"hello_world": {
"en": "Hello, World!",
"sp": "Hola, mundo"
},
"button_ok": {
"en": "Yes",
"sp": "Si"
},
"button_cancel": {
"en": "Cancel",
"sp": "Cancelar"
}
}
JSONLoad.cs
public class JSONLoader
{
private static readonly string _directory = "Langs/";
private static readonly string _fileName = "lang";
private string ReadJsonFile() { return Resources.Load<TextAsset>(_directory + _fileName).text; }
public void Load()
{
var file = ReadJsonFile();
var lang = JsonUtility.FromJson<LanguageObject>(file);
Debug.Log(lang);
}
}
[Serializable]
public class LangValue
{
public string en { get; set; }
public string sp { get; set; }
}
[Serializable]
public class LangKey
{
public string id { get; set; }
public List<LangValue> children { get; set; }
}
[Serializable]
public class LanguageObject
{
public List<LangKey> languageList { set; get; }
}
