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