I'm trying to load the data.json file for a unity project. I don't know where I went wrong. I want to be able to load the level: level1, level2 I have tried some other ways but still not working. Can anyone review and fix the error for me.
I create LoadDataLevel() to handle level and load data I create LevelLoadData which contains the elements of the level object
This is my C#:
public class BaseGameController : MonoBehaviour
{
public List<Level> levels;
public TextAsset textAsset;
public void LoadDataLevel()
{
JSONObject jsObj = (JSONObject)JSON.Parse(textAsset.text);
for (int i = 1; i <= 100; i++)
{
string key1 ="level"+i;
var jsonLevelLoad = jsObj[key1].AsObject;
levels = new List<Level>();
if (jsonLevelLoad != null)
{
for (int j = 1; j < jsonLevelLoad.Count; j++)
{
if (jsonLevelLoad[j] != null)
{
Level level = new Level();
level.LevelHeight = jsonLevelLoad[i][0].AsInt;
var Stars = jsonLevelLoad[j][1].AsArray;
if (Stars != null && Stars.Count > 0)
{
for (int k = 0; k < Stars.Count; k++)
{
if (Stars[k] != null)
level.Stars.Add(Stars[k].AsInt);
}
}
level.levelId = jsonLevelLoad[j][2].AsInt;
var Objects = jsonLevelLoad[j][3].AsArray;
if (Objects != null && Objects.Count > 0)
{
for (int h = 1; h < Objects.Count; h++)
{
Debug.Log(Objects[h]);
}
}
level.LevelTime = jsonLevelLoad[j][4].AsInt;
level.LevelWidth = jsonLevelLoad[j][5].AsInt;
levels.Add(level);
}
}
}
}
}
}
[System.Serializable]
public class LevelLoadData
{
public int LevelHeight;
public List<int> Stars = new List<int>();
public int levelId;
public List<Object> Objects = new List<Object>();
public int LevelTime;
public int LevelWidth;
}
This is my data.json It is list object
{
"level1": {
"LevelHeight": 320,
"Stars": [],
"Objects":[],
"LevelID": 85,
"LevelTime": 90,
"LevelWidth": 820
},
"level2": {
"LevelHeight": 320,
"Stars": [],
"Objects":[],
"LevelID": 88,
"LevelTime": 90,
"LevelWidth": 1170
}
}
First of all your loops / indices make not much sense to me.
You are iterating over
so assuming there are exactly
100
itemslevelX
in your json object but you are telling us there are only 2 right now.And then you try and access
which makes no sense since the
jsonLevelLoad
already is the element at indexi
. Also the iteration overmakes no sense since you already try and index the individual fields by hardcoded indices anyway.
So if something it should probably rather simply be
However I would rather go by he actual names and do
Then you are also for each iteration creating a new levels list
so you will always at maximum end up with one single level item.
You want to do this only ONCE.
So if you really want to go fully "manual" here it should probably rather be e.g.
However, in general instead of parsing this all "manually" I would suggest you rather use
Newtonsoft Json.NET
which comes as a Package via the Package manager and is even pre-installed in latest Unity versions.Then the JSON data structure you have is basically simply a dictionary so you could use
Note that in both examples I explicitly omitted the
Objects
field.Why? > Because in case this is a
UnityEnine.Object
it is completely unclear to me what exactly you have standing in your JSON file and how exactly you are going to create these.UnityEngine.Object
itself is not serializable into JSON since it will only store a reference and not actually perform a deep serialization I guess.