• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

how to read that JSON structure

F

farkraj

Guest
Hi, i am not experienced at all in writing/reading json files, hovewer i managed to save my grid in a nested structure but i have no idea how to read it.. i tried but failed miserably multiple times.

my structure looks like this:
Code:
{"grid":[[[4],0],[[5],1],[[3],2],[[4],3]}
which means [ [ type ] ID ]

handling jsons is so confusing most of the time i have no idea what im doing thus the problem. Can somebody help and tell me how to get that type and ID from that structure?
 

FrostyCat

Redemption Seeker
There is nothing confusing about JSON once you start looking at one depth at a time. Start at the highest depth and collapse everything deeper (i.e. any nested [] or {} structures). Iteratively find out where your target is in the current depth, bubble down, and repeat until you reach it.

Assume that you want to look for the 5 in your sample. You now note that the 5 is somewhere inside the ... filed under "grid".
Code:
{"grid": [[[4],0],[[5],1],[[3],2],[[4],3]}
{"grid": ...}
Now consider the next depth. The 5 is inside the second ..., hence you note 1 for its position at this depth.
Code:
[[[4],0],[[5],1],[[3],2],[[4],3]
[..., ..., ..., ...]
Inside that, the 5 is inside the first ..., hence you note 0 for its position at this depth.
Code:
[[5],1]
[..., 1]
You now have a lone list with a single entry, hence you note 0 for its position at this final depth.
Code:
[5]
This gives a sequence of "grid" => 1 => 0 => 0. Assuming that you have already decoded it into json_data:
Code:
var five;
five = json_data[? "grid"];
five = five[| 1];
five = five[| 0];
five = five[| 0];
show_message(five); // Shows 5
 
F

farkraj

Guest
It kinda works, only when i call it single time and hardcore the numbers. When i try to get ID with the loop it just doesnt work, says it is expecting a number. What do i do wrong? Btw. thanks for in depth explanation, its clearer now but still i have some issues.
Code:
for (var i = 0; i < total_tiles; i++)
{
   list = list[| i];
   list = list[| 0];
}
 

FrostyCat

Redemption Seeker
That's because you kept digging deeper and deeper without returning back up. You need to go back up before each iteration.
Code:
for (var i = 0; i < total_tiles; i++) {
  list = json_data[? "grid"]; // Go back up
  list = list[| i];
  list = list[| 0];
}
 
Top