简体   繁体   中英

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

According to the docs , JSON serialization using UnityEngine.JsonUtility relies on models having public fields , not properties . You'll need to define your models with fields instead of properties.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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