How often do you find the developer of something asking for help on code that he wrote for you to use…
HELP!!!
Here is my routine:
function M.saveTable(t, filename)
local path = system.pathForFile( filename, system.DocumentsDirectory)
local file = io.open(path, "w")
print("in saveTable. I think my table is....")
M.print\_r(t)
if file then
local contents = json.encode(t)
print("json data is")
print("\*" .. contents .. "\*")
file:write( contents )
io.close( file )
return true
else
return false
end
end
This has been working flawlessly for me and many of you… until now.
I’m building a game where you can have multiple matches going on. Each match is ID’ed with a big hex number like:
t[50778c04ec27b6000f00066b] = {
[1] = {question=“somestring”, answer=true},
[2] = {question=“someother string”, answer=false}
}
t[50778c04ec27b6000f00077c] = {
[1] = {question=“somestring else”, answer=true},
[2] = {question=“yet someother string”, answer=false}
}
When I JSON encode “t” I get an empty string. Now it’s not quite that simple as all my values are of course coming from other variables.
I wrote a test case, figuring I found a bug… and found it’s working as I thought:
json = require("json")
mytable = {}
a = "50778c04ec27b6000f00066b"
mytable[a] = {}
mytable[a][1] = {}
mytable[a][1]["question"] = "RYKF?"
print("\*" .. json.encode(mytable) .. "\*")
outputs:
*{“50778c04ec27b6000f00066b”:[{“question”:“RYKF?”}]}*
So my first thought is that for some reason that big hex number might be treated as a number, not a string. I know Lua tables don’t like t[1] = nil; t[2] = “something” that many things see that first entry as nil and assumes there is no other data.
The bright light went on, so I went and changed all the the top level key’s to be:
t[tostring(reallybighexnumber)]
So it would be a table not an array and that would solve my problem. BZZZT wrong answer. Tonight I tried forcing it to be a string by concatenating that big hex number to the letter “Z” (i.e. “Z” … matchID and that’s in a variable so it should work as a key.
Sure enough when I run my print_r function, I see my table as I expect it, but when I json.encode it, I get an empty string.
I’m stumped. Why can’t json.encode deal with this table?
Oh here is the console dump from my function at the top:
2012-10-12 22:02:45.833 Corona Simulator[733:707] in saveTable. I think my table is....
2012-10-12 22:02:45.833 Corona Simulator[733:707] table: 0x106232710 {
2012-10-12 22:02:45.834 Corona Simulator[733:707] [Z50778c04ec27b6000f00066b] =\> table: 0x106232710 {
2012-10-12 22:02:45.835 Corona Simulator[733:707] [1] =\> table: 0x106229800 {
2012-10-12 22:02:45.835 Corona Simulator[733:707] [question] =\> "TEST?"
2012-10-12 22:02:45.835 Corona Simulator[733:707] }
2012-10-12 22:02:45.836 Corona Simulator[733:707] }
2012-10-12 22:02:45.836 Corona Simulator[733:707] }
2012-10-12 22:02:45.836 Corona Simulator[733:707] json data is
2012-10-12 22:02:45.837 Corona Simulator[733:707] \*[]\*
The table looks like it should be saving properly, but json.encode is blowing up.
EDIT: Well this isn’t really resolved, but I switched to using SQLlite for this since I need a key lookup system rather than a big table to look through. But I think MY Developers found the problem with the cyclic table reference and that was likely what was breaking json.encode.
[import]uid: 19626 topic_id: 31912 reply_id: 331912[/import]