saving and loading 2D array using JSON

Hey Everyone

I am trying to save and load a 2D array I created. I am using the JSON library and I can get it to save my table fine but just can’t figure out how to load it.

Here is my code, this is my 2D array with 1’s and 0’s

    grid = {} --Grid array    

    – build screen now –

    for x = 0, across do

    grid[x] = {}

    for y = 0, down do

    local probability = math.random(0,10)

    if probability <= stonefrequency then

    grid[x][y] = 1 —Stone    

    else

    grid[x][y] = 0 --Dirt

    end

    end

    end

Here is my save call

loadsave.saveTable(grid, “grid.json”)

Now I exit my app and reopen it loading my JSON data

gridsave = loadsave.loadTable(“grid.json”)

I just don’t know how to feed my loaded data into a new 2d grid. I have gotten it to work if I do the save and load all in one scene. But once I close my app and try to build the array with saved data it doesn’t work.

Any help would be great.

It could be as easy as:

[lua]

local json = require( “json” )

local data = loadsave.loadTable( “grid.json” )

local grid = json.decode( data )

[/lua]

But I’m not sure what your saveTable data looks like.

Cheers.

my table is just a 100x100 grid of 1’s and 0’s

how can I input my json data in a new table?

I tried

gridsave = loadsave.loadTable(“grid.json”)

I have tested printing my saved JSON file and I get 1’s and 0’s but only if I do the save and load in the same scene.

I tired this

for x = 1, across do

grid[x] = {}

for y = 1, down do

grid[x][y] = gridsave[x][y] ----this is my loaded data but this is null if I close and reopen my app.

end

end

I tested this by creating a scene were I created a small 2x2 array of 2’s and 8’s then I saved that array using JSON and then loaded it. I was able to print out the old array and the new array perfectly. But when I close the app and reopen it and then try to load my array I can even print out the data.

Where did you get the loadsave module from?  If it’s the one I found it should work when you do this:

[lua]

–save

loadsave.saveTable(grid, “grid.json”)

–later on when you reload the app

grid = loadsave.loadTable(“grid.json”)

print( “GRID”, grid[1][1] )

[/lua]

Does that still give you an issue?

I think I got it figured out, I was starting my array at 0 and not 1 which I guess screws it up.

Yes it is fixed, I didn’t notice that I was creating my array starting at 0 and that was messing it up. Thanks for all the help!

Yes, a common LUA issue.  You get used to it.  Glad it got worked out.  :slight_smile:

Cheers.

It could be as easy as:

[lua]

local json = require( “json” )

local data = loadsave.loadTable( “grid.json” )

local grid = json.decode( data )

[/lua]

But I’m not sure what your saveTable data looks like.

Cheers.

my table is just a 100x100 grid of 1’s and 0’s

how can I input my json data in a new table?

I tried

gridsave = loadsave.loadTable(“grid.json”)

I have tested printing my saved JSON file and I get 1’s and 0’s but only if I do the save and load in the same scene.

I tired this

for x = 1, across do

grid[x] = {}

for y = 1, down do

grid[x][y] = gridsave[x][y] ----this is my loaded data but this is null if I close and reopen my app.

end

end

I tested this by creating a scene were I created a small 2x2 array of 2’s and 8’s then I saved that array using JSON and then loaded it. I was able to print out the old array and the new array perfectly. But when I close the app and reopen it and then try to load my array I can even print out the data.

Where did you get the loadsave module from?  If it’s the one I found it should work when you do this:

[lua]

–save

loadsave.saveTable(grid, “grid.json”)

–later on when you reload the app

grid = loadsave.loadTable(“grid.json”)

print( “GRID”, grid[1][1] )

[/lua]

Does that still give you an issue?

I think I got it figured out, I was starting my array at 0 and not 1 which I guess screws it up.

Yes it is fixed, I didn’t notice that I was creating my array starting at 0 and that was messing it up. Thanks for all the help!

Yes, a common LUA issue.  You get used to it.  Glad it got worked out.  :slight_smile:

Cheers.