this question has answer here:
i'm trying populate 3 lists according json file.
the goal load data json file, assign lists can add/modify values , re-save them json file.
however there seems problem line
level.add(new level(i, (string)levelsjson[i]["name"]), props );
specifically props.
i appreciate solution on how populate levels list , modify!
this json structure
[ { "id": 0, "name": "greatest level", "props": [ { "name": "stone", "position": [ 4, 2 ] }, { "name": "tree", "position": [ 1, 7 ] } ] }, { "id": 1, "name": "shitty level", "props": [ { "name": "stone", "position": [ 2, -5 ] }, { "name": "tree", "position": [ 15, 2 ] } ] } ]
this entire code
using system.collections; using system.collections.generic; using unityengine; using system.io; using litjson; public class json : monobehaviour { private list<levels> levels = new list<levels>(); private jsondata levelsjson; void start () { loadlevels(); } void loadlevels() { levelsjson = jsonmapper.toobject(file.readalltext(application.datapath + "/streamingassets/levels.json")); //this im trying populate lists for(int = 0; < levelsjson.count; i++) { list<level> level = new list<level>(); list<prop> props = new list<prop>(); for(int prop = 0; prop < levelsjson[i]["props"].count; prop++) { props.add(new prop((string)levelsjson[i]["props"]["name"], new int[] { (int)levelsjson[i]["props"]["position"][0], (int)levelsjson[i]["props"]["position"][1]})); } level.add(new level(i, (string)levelsjson[i]["name"]), props ); } } public class levels { public level[] levels; public levels(level[] l) { levels = l; } } public class level { public int id; public string name; public prop[] props; public level(int i, string n, prop[] p) { id = i; name = n; props = p; } } public class prop { public string name; public int[] position; public prop(string n, int[] p) { name = n; position = p; } }
one issue see you're initializing level list inside of initial loop populate level list. cause level
variable re-initialized new empty list @ start of each iteration.
try moving line: list<level> level = new list<level>();
before line: for(int = 0; < levelsjson.count; i++) {
Comments
Post a Comment