How can I use Table-Load-Save-Functions?

Can somebody correct my Table-Load-Save code? Please~~

It can’t work. Thank you very much.

My code is below : 

local json = require(“json”)

local save

local load

local user = {}

function save(filename, userValue)

    local path = system.pathForFile( filename, system.DocumentsDirectory)

    local file = io.open(path, “w”)

    if file then

        local contents = json.encode(userValue)

        file:write( contents )

        io.close( file )

    end

end

  

function load(filename)

    local path = system.pathForFile( filename, system.DocumentsDirectory)

    local contents = “”

    local userValue = {}

    local file = io.open( path, “r” )

    if file then

         local contents = file:read( “*a” )

        userValue = json.decode(contents);

         io.close( file )

         return userValue

     else 

       userValue.bloodBarLength = 300

       userValue.coin = 0

        save(userValue, “userValue.json”)

       return userValue

    end

end

user = load(“user.json”)

print(user.bloodBarLength)

print(user.coin)

save(userValue, “userValue.json”)

Take a look at my iolib library here: http://code.coronalabs.com/code/io-lib

To save a table to the Documents directory:

local json = require("json") local iolib = require("iolib") iolib.wrDocs( "mytable.json", json.encode( mytable ) )

To load a table from the Documents directory:

local json = require("json") local iolib = require("iolib") local mytable = json.decode( iolib.wrDocs( "mytable.json" ) )

horacebury

Thank you very much. I am studying it.

Have you seen this tutorial?

http://coronalabs.com/blog/2014/10/14/tutorial-saving-and-loading-lua-tables-with-json/

Rob

> Rob

It looks very useful~~

I  have to figure out the concept of JSON thanks for recommending this tutorial

Take a look at my iolib library here: http://code.coronalabs.com/code/io-lib

To save a table to the Documents directory:

local json = require("json") local iolib = require("iolib") iolib.wrDocs( "mytable.json", json.encode( mytable ) )

To load a table from the Documents directory:

local json = require("json") local iolib = require("iolib") local mytable = json.decode( iolib.wrDocs( "mytable.json" ) )

horacebury

Thank you very much. I am studying it.

Have you seen this tutorial?

http://coronalabs.com/blog/2014/10/14/tutorial-saving-and-loading-lua-tables-with-json/

Rob

> Rob

It looks very useful~~

I  have to figure out the concept of JSON thanks for recommending this tutorial