Trouble using JSON to recall previously entered values

I was using this article to try to have my application remember data a user entered the last time they used the application (

After trying it out on an android and not seeing what I expected, through in a lot of “print” statements and I think the problem is that it’s not saving my table. 

(note, when I say terminal below, I’m referring to the terminal for corona’s simulator)

I appreciate any help

[lua] local json = require(“json”)

function saveTable(t, filename)

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

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

    if file then

        local contents = json.encode(t)

        file:write( contents )

        io.close( file )

        return true

    else

        return false

    end

end

function loadTable(filename)

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

    local contents = “”

    local myTable = {}

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

    if file then

         – read all contents of file into a string

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

         myTable = json.decode(contents);

         io.close( file )

         return myTable 

    end

    return nil

end

cacheGroup = {} [/lua]

[lua]

print (" this is Input.text "… Input.text) ------> terminal says “10”

cacheGroup.newInput = Input.text

print ("this is cacheGroup.newInput after setting equal to Input "…cacheGroup.newInput) —> terminal says “10”

saveTable(cacheGroup, “cacheGroup.json”)

print ("Input.text =  "… Input.text) -----> terminal says “10”

print ("cacheGroup.currentTaxValue =  " … cacheGroup.currentTaxValue) -----> terminal says “10”

cacheGroup = loadTable(“cacheGroup.json”)

print (cacheGroup.currentTaxValue) ------> terminal says “nil”

[/lua]

I would suggest getting a table printing function and dumping your whole table.  We recently did a tutorial on table printing that might be of help for you.

I would print the table before you save it, after you save it and after you load it.

You can also open your apps sandbox if you’re doing this in the Corona Simulator to test, then the files can easily be looked at.

Here’s the link that Rob was mentioning:  http://coronalabs.com/blog/2014/09/02/tutorial-printing-table-contents/

The print_r function will print everything in the table recursively.

Thanks guys, I was able to get the pairs() function to give me what I needed but an error kept coming up when I tried print_r even after I tried table.print = print_r.

What fixed the problem was if I added my newInput directly into my group (in my line 31)

I would suggest getting a table printing function and dumping your whole table.  We recently did a tutorial on table printing that might be of help for you.

I would print the table before you save it, after you save it and after you load it.

You can also open your apps sandbox if you’re doing this in the Corona Simulator to test, then the files can easily be looked at.

Here’s the link that Rob was mentioning:  http://coronalabs.com/blog/2014/09/02/tutorial-printing-table-contents/

The print_r function will print everything in the table recursively.

Thanks guys, I was able to get the pairs() function to give me what I needed but an error kept coming up when I tried print_r even after I tried table.print = print_r.

What fixed the problem was if I added my newInput directly into my group (in my line 31)