I currently have a JSON file:
{
"locations": [
{
"name": "Mars",
"type": "mining",
"metals": 500,
"gas": 0
},
{
"name": "Jupiter",
"type": "gas",
"metals": 0,
"gas": 1000
}
]
}
And a script that reads it:
public class JSONReader : MonoBehaviour
{
public TextAsset textJSON;
[Serializable]
public class Location
{
public string name;
public string type;
public int metals;
public int gas;
}
[Serializable]
public class LocationArray
{
public Location[] locations;
}
public LocationArray mLocationArray = new LocationArray();
public Location[] locationArray;
void Start()
{
mLocationArray = JsonUtility.FromJson<LocationArray>(textJSON.text);
locationArray = mLocationArray.locations;
foreach (Location loc in locationArray)
{
Debug.Log(loc.name);
}
I'm intending to modify the JSON file to add more locations and also nest the planets in star systems, however I'm not quite sure how to modify my script to accommodate the nesting. I've read that JSONUtility cannot output 2-dimensional arrays, however is that the only way that nested JSON data can be handled? Do I absolutely need to use a third-party library for this?