1
\$\begingroup\$

I'm using Unity and trying to load in data from JSON files. It finds the files, but no data comes through. when I do a Debug.Log(count) on it it tells me there are zero entries. But the same code for planets works fine. (I haven't got any code in the agent's field yet).

I even used a wrapper like you're supposed to. Here are the object and its wrapper:

    public class StructureList
    {
        public List<Structure> structures = new List<Structure>();
    }

    public class Structure
    {

        public int id;
        public string structureName;
        public int productionMaterials; 
        public int productionPersonnel;
        public int defensiveShields;
        public int offensiveDamage;
        public int defensiveIonDamage;
        public int constructionShip;
        public int constructionStructure;
        public int constructionArmy;
        public int detectionFleet;
        public int detectionAgents;
        public string structureDescription;
        public int colonyId;
        public int costCrew;
        public int costMaterials;
    }

here's the json file:

{
  "structures": [
    {
      "id": 0,
      "structureName": "Factory",
      "productionMaterials": 5,
      "productionPersonnel": 0,
      "defensiveShields": 0,
      "offensiveDamage": 0,
      "defensiveIonDamage": 0,
      "constructionShip": 0,
      "constructionStructure": 0,
      "constructionArmy": 0,
      "detectionFleet": 0,
      "detectionAgents": 0,
      "structureDescription": "A basic factory that provides construction materials to the empire.",
      "costCrew": 100,
      "costMaterials": 200,
      "colonyId": 0
    },
    {
      "id": 1,
      "structureName": "Recruitment Center",
      "productionMaterials": 0,
      "productionPersonnel": 5,
      "defensiveShields": 0,
      "offensiveDamage": 0,
      "defensiveIonDamage": 0,
      "constructionShip": 0,
      "constructionStructure": 0,
      "constructionArmy": 0,
      "detectionFleet": 0,
      "detectionAgents": 0,
      "structureDescription": "A basic structure that recruits personnel for employ in the empire.",
      "costCrew": 200,
      "costMaterials": 100,
      "colonyId": 0
    },
    {
      "id": 2,
      "structureName": "Shield Generator",
      "productionMaterials": 0,
      "productionPersonnel": 0,
      "defensiveShields": 10,
      "offensiveDamage": 0,
      "defensiveIonDamage": 0,
      "constructionShip": 0,
      "constructionStructure": 0,
      "constructionArmy": 0,
      "detectionFleet": 0,
      "detectionAgents": 0,
      "structureDescription": "A planetary shield generator that will defend against orbital bombardment. Can defend against incursions if their are multiple.",
      "costCrew": 25,
      "costMaterials": 200,
      "colonyId": 0
    },
    {
      "id": 3,
      "structureName": "SOL Platform",
      "productionMaterials": 0,
      "productionPersonnel": 0,
      "defensiveShields": 0,
      "offensiveDamage": 10,
      "defensiveIonDamage": 0,
      "constructionShip": 0,
      "constructionStructure": 0,
      "constructionArmy": 0,
      "detectionFleet": 0,
      "detectionAgents": 0,
      "structureDescription": "A Surface to Orbit laser turret that fires at ships which come close enough for orbital bombardments.",
      "costCrew": 25,
      "costMaterials": 350,
      "colonyId": 0
    },
    {
      "id": 4,
      "structureName": "Ion Cannon",
      "productionMaterials": 0,
      "productionPersonnel": 0,
      "defensiveShields": 0,
      "offensiveDamage": 0,
      "defensiveIonDamage": 5,
      "constructionShip": 0,
      "constructionStructure": 0,
      "constructionArmy": 0,
      "detectionFleet": 0,
      "detectionAgents": 0,
      "structureDescription": "A low orbit station designed to build space worthy ships.",
      "costCrew": 25,
      "costMaterials": 250,
      "colonyId": 0
    },
    {
      "id": 5,
      "structureName": "Shipyard",
      "productionMaterials": 0,
      "productionPersonnel": 0,
      "defensiveShields": 0,
      "offensiveDamage": 0,
      "defensiveIonDamage": 0,
      "constructionShip": 10,
      "constructionStructure": 0,
      "constructionArmy": 0,
      "detectionFleet": 0,
      "detectionAgents": 0,
      "structureDescription": "A base of operations with construction crews for building other structures.",
      "costCrew": 500,
      "costMaterials": 250,
      "colonyId": 0
    },
    {
      "id": 6,
      "structureName": "Construction Yard",
      "productionMaterials": 0,
      "productionPersonnel": 0,
      "defensiveShields": 0,
      "offensiveDamage": 0,
      "defensiveIonDamage": 0,
      "constructionShip": 0,
      "constructionStructure": 10,
      "constructionArmy": 0,
      "detectionFleet": 0,
      "detectionAgents": 0,
      "structureDescription": "A training center that turns personnel into armies. Also, helps detect enemy agents.",
      "costCrew": 500,
      "costMaterials": 500,
      "colonyId": 0
    },
    {
      "id": 7,
      "structureName": "Barracks",
      "productionMaterials": 0,
      "productionPersonnel": 0,
      "defensiveShields": 0,
      "offensiveDamage": 0,
      "defensiveIonDamage": 0,
      "constructionShip": 0,
      "constructionStructure": 0,
      "constructionArmy": 5,
      "detectionFleet": 0,
      "detectionAgents": 5,
      "structureDescription": "An empty planet, devoid of life.",
      "costCrew": 250,
      "costMaterials": 500,
      "colonyId": 0
    },
    {
      "id": 8,
      "structureName": "Sensor Array",
      "productionMaterials": 0,
      "productionPersonnel": 0,
      "defensiveShields": 0,
      "offensiveDamage": 0,
      "defensiveIonDamage": 0,
      "constructionShip": 0,
      "constructionStructure": 0,
      "constructionArmy": 0,
      "detectionFleet": 10,
      "detectionAgents": 0,
      "structureDescription": "Detects fleets at a distance, incoming to the planet.",
      "costCrew": 10,
      "costMaterials": 500,
      "colonyId": 0
    },
    {
      "id": 9,
      "structureName": "Communications Array",
      "productionMaterials": 0,
      "productionPersonnel": 0,
      "defensiveShields": 0,
      "offensiveDamage": 0,
      "defensiveIonDamage": 0,
      "constructionShip": 0,
      "constructionStructure": 0,
      "constructionArmy": 0,
      "detectionFleet": 0,
      "detectionAgents": 10,
      "structureDescription": "Used to decrypt messages being sent out from the planet, in order to detect enemy agents.",
      "costCrew": 150,
      "costMaterials": 50,
      "colonyId": 0
    }
  ]
}

And here's my loading code:


        public static PlanetList planets;
        public static StructureList structures;
        public static AgentList agents;

        public static void LoadData()
        {
            string planetPath = Application.dataPath + "/Resources/PlanetDB.json";
            string structurePath = Application.dataPath + "/Resources/StructureDB.json";
            string agentPath = Application.dataPath + "/Resources/AgentDB.json";

            if (File.Exists(planetPath))
            {
                string planetsdataAsJson = File.ReadAllText(planetPath);
                planets = JsonUtility.FromJson<PlanetList>(planetsdataAsJson);
                Debug.Log("Planets Loaded: " + planets.planets.Count);
            }
            else
            {
                planets = new PlanetList();
                Debug.LogError("FAILED TO LOAD PLANETDATA");
            }

            if (File.Exists(structurePath))
            {
                string structuresdataAsJson = File.ReadAllText(structurePath);
                structures = JsonUtility.FromJson<StructureList>(structuresdataAsJson);
                Debug.Log("Structures Loaded: " + structures.structures.Count);
            }
            else
            {
                structures = new StructureList();
                Debug.LogError("FAILED TO LOAD STRUCUREDATA");
            }

            if (File.Exists(agentPath))
            {
                string agentsdataAsJson = File.ReadAllText(agentPath);
                agents = JsonUtility.FromJson<AgentList>(agentsdataAsJson);
                Debug.Log("Agents Loaded: " + agents.agents.Count);
            }
            else
            {
                agents = new AgentList();
                Debug.LogError("FAILED TO LOAD AGENTDATA");
            }
        }
```
\$\endgroup\$
3
\$\begingroup\$

You forgot to add [Serializable] attribute :)

From https://docs.unity3d.com/ScriptReference/JsonUtility.FromJson.html:

It must be a plain class/struct marked with the Serializable attribute.

Add the attributes and it will deserialize:

[Serializable]
public class StructureList
{
    public List<Structure> structures = new List<Structure>();
}

[Serializable]
public class Structure
{
    public int id;
    public string structureName;
    public int productionMaterials;
    public int productionPersonnel;
    public int defensiveShields;
    public int offensiveDamage;
    public int defensiveIonDamage;
    public int constructionShip;
    public int constructionStructure;
    public int constructionArmy;
    public int detectionFleet;
    public int detectionAgents;
    public string structureDescription;
    public int colonyId;
    public int costCrew;
    public int costMaterials;
}
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Doh! Thank you :) \$\endgroup\$ – DauntlessRob Apr 18 '19 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.