简体   繁体   English

访问子对象时序列化 JSON 文件返回空对象

[英]Serializing a JSON file returns null object when acccessing child objects

I'm trying to read in a local JSON file that contains various objects.我正在尝试读取包含各种对象的本地 JSON 文件。 But the object always returns null.但是对象总是返回null。

I know that the Resources.Load<TextAsset>(_directory + _fileName).text;我知道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 .即: hello_world.sp将返回Hola, mundo

However, anytime I go to access any object like Debug.Log(lang.languageList.Count);但是,无论何时我去访问任何对象,如Debug.Log(lang.languageList.Count); I get the error:我收到错误:

NullReferenceException: Object reference not set to an instance of an object NullReferenceException:未将对象引用设置为对象的实例

Eventually, I would like to be able to add additional language values, fr , it , etc...最终,我希望能够添加额外的语言值, frit等......

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 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; }
    }

According to the docs , JSON serialization using UnityEngine.JsonUtility relies on models having public fields , not properties .根据文档,使用UnityEngine.JsonUtility JSON 序列化依赖于具有公共字段而不是属性的模型 You'll need to define your models with fields instead of properties.您需要使用字段而不是属性来定义模型。

Ie., IE。,

[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;
}

Your Json structure doesn't match your classes.您的 Json 结构与您的课程不匹配。 It's not a list.这不是一个列表。 Here's a working example:这是一个工作示例:

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"
            }
        }
    ]
}

Classes:课程:

[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: I'm not a fan of JsonUtility I'd rather go for Json.Net which allows the use of properties among other things. PS:我不是JsonUtility的粉丝,我宁愿选择Json.Net ,它允许使用属性等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM