I have been using Rob Miracle’s code to save and load data with JSON. The problem I run into is when changing scenes in composer or re running my program.
What I have is some buttons that add some number values to a temporary table. Then a submit button that loads the JSON table, then checks some conditions and either adds the temporary table to the table that holds the loaded JSON table, then the table is saved back into the JSON file.
I can print the table in that scene and it recognizes the values, no problem. Problems occur when, like I mentioned above, change scenes or reload the program.
The JSON table looks like this:
When entering the data the first time - [{“1”:11,“2”:44,“3”:60,“date”:“140529”}]
When entering a second time - [{“1”:33,“2”:109,“3”:75,“date”:“140529”,“1”:11,“3”:60,“2”:44}]
As you can see it isn’t reading or saving the data correctly… and I’m not really sure where my problem is at. Also, when printing the table after adding the second time it only recognizes the newly input data.
If the explanation wasn’t clear enough, here is some relevant code where this is going down:
local function confirmHandler(event) local buyinTable = t.loadTable("buyins.json") tempTable.date = os.date("%y".."%m".."%d") local function checkDate(table, date) local match = false local key if table then for i = 1, #table do if table[i].date == date then match = true key = i end end else buyinTable = {} end if match == true then return true, key elseif match == false then return false end end condition, key = checkDate(buyinTable, tempTable.date) print(condition, key) if condition == true then for i = 1, #tempTable do -- table.insert( buyinTable[key], tempTable[i] ) buyinTable[key][#buyinTable[key]+1] = tempTable[i] end print(table.concat( tempTable, ", " )) print("a "..table.concat( buyinTable[key], ", " )) elseif condition == false then table.insert(buyinTable, tempTable) print(table.concat( tempTable, ", " )) print("b "..table.concat( buyinTable[#buyinTable], ", " ).." date: "..buyinTable[#buyinTable].date) end t.saveTable(buyinTable, "buyins.json") clearText() composer.gotoScene("menu") end
The confirmHandler function is called when the submit button is pressed.
Thanks in advance, let me know if you need any further explanation.