【发布时间】:2020-06-17 04:31:15
【问题描述】:
我正在尝试读取包含各种对象的本地 JSON 文件。但对象总是返回 null。
我知道Resources.Load<TextAsset>(_directory + _fileName).text; 已成功找到该文件,因为我可以将文本输出到控制台。
我的目标是能够请求密钥并取回所选语言的值。即:hello_world.sp 将返回 Hola, mundo。
但是,每当我访问像 Debug.Log(lang.languageList.Count); 这样的任何对象时,我都会收到错误消息:
NullReferenceException:对象引用未设置为 对象
最终,我希望能够添加其他语言值,fr、it 等...
谁能看出我做错了什么?
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; }
}
【问题讨论】:
-
我确定不支持属性..
标签: c# json unity3d serialization