【问题标题】:Serializing a JSON file returns null object when acccessing child objects访问子对象时,序列化 JSON 文件返回空对象
【发布时间】: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:对象引用未设置为 对象

最终,我希望能够添加其他语言值,frit 等...

谁能看出我做错了什么?

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


【解决方案1】:

您的 Json 结构与您的类不匹配。这不是一个清单。这是一个工作示例:

json:

{
    "languageList": 
     [
        {
            "id": "hello_world",
            "children": {
                "en": "Hello, World!",
                "sp": "Hola, mundo"
            }
        },
        {
            "id": "button_ok",
             "children": {
                "en": "Yes",
                "sp": "Si"
            }
        },
        {
            "id": "button_cancel",
            "children": {
                "en": "Cancel",
                "sp": "Cancelar"
            }
        }
    ]
}

类:

[Serializable]
public class LangValue
{
    public string en;
    public string sp;
}

[Serializable]
public class LangKey
{
    public string id;
    public LangValue children;
}

[Serializable]
public class LanguageObject
{
    public List<LangKey> languageList;
}

PS:我不是JsonUtility 的粉丝,我宁愿选择Json.Net,它允许使用属性等。

【讨论】:

    【解决方案2】:

    根据the docs,使用UnityEngine.JsonUtility 的JSON 序列化依赖于具有公共字段 的模型,而不是属性。您需要使用字段而不是属性来定义模型。

    即,

    [Serializable]
    public class LangValue
    {
        public string en;
        public string sp;
    }
    
    [Serializable]
    public class LangKey
    {
        public string id;
        public List<LangValue> children;
    }
    
    [Serializable]
    public class LanguageObject
    {
        public List<LangKey> languageList;
    }
    

    【讨论】:

    • facepalm 我不敢相信我没听懂。我整个早上都在拉头发。谢谢。
    • @user-44651 这也行不通,问题是您的 json 与您尝试反序列化的类结构不匹配。它甚至不包含列表/数组!
    猜你喜欢
    • 2022-01-14
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    相关资源
    最近更新 更多