How to import a list or array of objects from a json file?

I’m trying to load a games information from a JSON file, with the idea being to make a custom editor window to edit the info.

There are multiple json files (heroes.json, equipment.json, spells.json, enemies.json etc) which I’m trying to load into one database class.


The json is formatted something like this (Tho I’ve edited it to shorten the amount of info. If there is a typo in the JSON ignore, as I know the JSON works fine):

{ "Hero" : [
{"id":1,"name":"Bob","class":1,"level":1},
{"id":2,"name":"Jane","class":3,"level":1} ] }

My hero class is serialized:

[System.Serializable]
public class Hero
{
    private int id;
    private string name;
    private int class;
    private int level
}

As is my database class:

[System.Serializable]
public class DB
{
    public Hero[] heroes;
    public Class[] classes;
    //...Other data
}

I have a JSON handler class that reads the JSON and returns it as a string… (as you can see, I tried to use both streamreader and TextAsset…)

public class JSONHandler 
{
    private string dataLocation = Application.dataPath + "/Resources/data/";
    private string persistantDataLocation = Application.persistentDataPath + "/Resources/data/";

    public string LoadFromJson(string fileName)
    {
        if (File.Exists(Application.dataPath + "/Resources/data/" + fileName + ".json"))
        {
            StreamReader sr = new StreamReader(dataLocation + fileName + ".json");
            string jsonString = sr.ReadToEnd();
            sr.Close();
            return jsonString;

     //   TextAsset jsonString = Resources.Load<TextAsset>("/data/" + fileName);
          //  if (jsonString != null)
          //  {
          //      Debug.Log("Json Loaded");
      //          return jsonString.text;
          //  }
          //  else
          //  {
          //      Debug.Log("Json Failed to Load");
          //      return null;
          //  }

        }
        else
        {
            Debug.Log("!--! ERROR: FALED TO LOAD "+fileName+".json FILE !--!");
            return null;
        }
    }
}

And finally, I’m trying to take that string and put it into my Unity class, so it can be displayed in the inspector / custom editor window…

public class Test : MonoBehaviour
{
    public DB db;
    void Start()
    {
        JSONHandler jh = new JSONHandler();
        string jsonHeroes = jh.LoadFromJson("Heroes");
        Debug.Log(jsonHeroes); // <-- Works fine. The string is returned as expected
        
        if (jsonHeroes != null)
        {
            db = JsonUtility.FromJson<DB>(jsonHeroes); // <-- Not working
        }
        else
        {
            Debug.Log("jsonHeroes string is Null");
        }
    }
}

I’m not getting any errors, however db is null after using JsonUtility() and my 2 heroes are not showing up in the inespector… I’m probably missing something basic. Any help?

In your json text you called your array “Hero”:

 { "Hero" : [

however in your DB class you called the array “heroes”:

public Hero[] heroes;

So this obviously won’t work.

Your second issues is you made all your fields inside your Hero class private. So none of them will be serialized or loaded. They have to be public