json file failing on iPad

I am using the code snippet at the bottom of this post in my code. It is from Rob Miracle. Very good code, by the way.

I am loading a small json file that contains a series of x,y points. All works well in the simulator. It reads the data in fine and maneuvers the object on screen across those x,y coords.

But, when same file is built and transferd to the iPad to test, it fails when processing the code related to the json file. I tested for 2 days now and I am certain the ‘loadtable’ function itself is not the issue, because, if I strip the code down to just the ‘load table’ function there are no issues on the iPad. But as soon as I try to access any field/value from that table it seems like the rest of the code below that code does not execute.

Even if I try just running an empty loop using the #theJsonTable… it fails on the iPad, but still runs well on the simulator.

I am assuming the simulator is more tolerant of things then the actual iPad is. Anything to do with that json table seems to have a bad effect on the iPad.

Anyone have similar issues using json on the iPad?? Or does anyone have any insight on this issue?

function loadTable(filename)
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

[import]uid: 148857 topic_id: 26778 reply_id: 326778[/import]

Is this a table indexed by numbers?

eg mytable[1] = 6

if so, when the json library reads it back in, it doesn’t seem to do it right.

you end up with a table indexed by strings

mytable[“1”] ==6

Examine the string you read in before you decode it.

[import]uid: 108660 topic_id: 26778 reply_id: 108712[/import]

Jeff,

Thanks for your help. Here is the actual table :

[{“y”:16,“x”:43},{“y”:79,“x”:65},{“y”:150,“x”:92},{“y”:209,“x”:152},{“y”:250,“x”:279},{“y”:274,“x”:403},{“y”:268,“x”:496},{“y”:212,“x”:553},{“y”:126,“x”:611},{“y”:67,“x”:679},{“y”:39,“x”:783},{“y”:47,“x”:895},{“y”:110,“x”:935},{“y”:183,“x”:899},{“y”:211,“x”:812}]
if I do a loop using ‘pairs’

for key,value in pairs(t) do
– key shows to be a number (in this case keys are 1 thru 15, as there 15 x,y coords here).
end
what still is confusing, is it works in the simulator and gets the correct x,y values from the file, but fails in the actual iPad.

Anyway, I am not real sure what to do about this even with your answer. Could you elaborate a little more, or maybe a sample of how I can properly read this table into the app?

[import]uid: 148857 topic_id: 26778 reply_id: 108728[/import]

How are you requiring the json library? [import]uid: 84637 topic_id: 26778 reply_id: 109097[/import]

This function is useful for debugging problems related to accessing table data. You can try printing out the table immediately after it is loaded to see what it got.

function printTable( t, label, level )  
-- print(" Printing Table:")  
 if label then print( label ) end  
 level = level or 1  
  
 if t then  
 for k,v in pairs( t ) do  
 local prefix = ""  
 for i=1,level do  
 prefix = prefix .. " " -- was "\t"  
 end  
  
 print( prefix .. "[" .. tostring(k) .. "] = " .. tostring(v) )  
 if type( v ) == "table" then  
 print( prefix .. "{" )  
 printTable( v, nil, level + 1 )  
 print( prefix .. "}" )  
 end  
 end  
 else  
 print(" - No Table -")  
 end  
end  

usage: printTable(myTable, “MyTable Title:”, 3 )

Above would print the table down to three levels deep (tables withing tables). Note that if used for tables that contain functions it can get lost in infinite recursion, but for normal data tables like yours, it works fine. [import]uid: 79933 topic_id: 26778 reply_id: 109102[/import]