Hey team! Im having trouble reading my table after downloading some external JSON.
Here, Im pretty sure everything is working fine:
Dump the old JSON data, Download new JSON, decode, and write to a table.
[lua]
function loadTable(filename)
path = system.pathForFile( filename, system.CachesDirectory)
contents = “”
myTable = {}
file = io.open( path, “r” )
if file then
contents = file:read( “*a” )
local myTable = json.decode(contents);
print( "Contents of " … path … “\n” … contents )
print ( “decoded new json” )
decoded = true
io.close( file )
return myTable
end
return nil
end
function parseChecker()
if decoded==true then
print (“lets get it started”)
Runtime:removeEventListener(“enterFrame”, parseChecker)
timer.performWithDelay( 100, initVars)
timer.performWithDelay( 300, prepareImages )
end
end
Runtime:addEventListener( “enterFrame”, parseChecker )
local function jsonListener( event )
if ( event.isError ) then
print ( “Network error - download failed” )
else
print (“json successfully downloaded”)
loadTable(“coupvals.json”)
end
end
function fetchJson()
network.download( “http://www.thisiskel.com/coupvals.json”, “GET”, jsonListener, “coupvals.json”, system.CachesDirectory )
end
function dumpJson()
local destDir = system.CachesDirectory – where the file is stored
local results, reason = os.remove( system.pathForFile( “coupvals.json”, destDir ) )
if results then
print( “old json removed” )
timer.performWithDelay( 100, fetchJson )
else
print( “file does not exist”, reason )
timer.performWithDelay( 100, fetchJson )
end
end
dumpJson()
[/lua]
I have an event listener (parseChecker) checking to see when the JSON gets written to a table, then starts doing what it wants with the data. However, after everything decodes, Im not sure how to read the values in the table or get the length of it.
when “initVars” runs, I try to get the length of the table or get keys within that table and get either “0” or “nil”. I’ve tried:
[lua]
print(#myTable)
print(myTable[1].logo)
print(myTable[1])
[/lua]
Heres my json
[{ "business": "Happy Haleiwa", "logo": "logo\_happyhaleiwa.png", "badge": "badgeFg", "detail":"thumb\_happyhaleiwa.png", "phone":"8089263011", "url":"http://happyhaleiwa.net/", "location": "355 – B Royal Hawaiian Avenue, Honolulu , HI 96815", "latitude":"21.283548", "longitude":"-157.830652", "H1":"Free Tote", "H2":"With any purchase $75 or more. Limit one per customer. Cannot be combined with any other offers. Supplies limited." }, { "business": "Egan's Fit Body Bootcamp", "logo": "logo\_egan.png", "badge": "badge15", "detail":"thumb\_egan.png", "url":"http://hawaiifitcamp.blogspot.com/" "phone":"8082713779", "location": "2851 E. Manoa Road, Honolulu, HI 96822", "latitude":"21.313128", "longitude":"-157.812410", "H1":"One Year Membership", "H2":"Offer ends 9/1/2012." }, { "business": "Hawaii Surf Terminal", "logo": "logo\_hst.png", "badge": "badge20", "detail":"thumb\_hst.png", "phone":"8084365480", "url":"http://hawaiisurfterminal.com/" "location": "3259 Koapaka Street, Honolulu, Hawaii 96819", "latitude":"21.339152", "longitude":"-157.916247", "H1":"First Year board storage", "H2":"Offer ends 9/1/2012." }, { "business": "Surfer girls Academy", "logo": "logo\_surferGirlAcademy.png", "badge": "badgeFg", "detail":"thumb\_surferGirlAcademy.png", "phone":"8084692001", "url":"http://www.surfergirlacademy.com" "location": "Discovery Bay Center, Honolulu, Hawaii 96815", "latitude":"21.288540", "longitude":"-157.841140", "H1":"Free tote bag for each individual person (value $20)", "H2":"Offer ends 12/1/2012." }, { "business": "RV's Ocean Sports", "logo": "logo\_rvs.png", "badge": "badgeFg", "detail":"thumb\_rvs.png", "phone":"8087327137", "url":"rvsocean.com", "location": "3348 Campbell Avenue, Honolulu, Hawaii 96815", "latitude":"21.281100", "longitude":"-157.816400", "H1":"Free Gift", "H2":"While supplies last!" }, { "business": "Menehune Mac Chocolate Factory", "logo": "logo\_menehunemac.png", "badge": "badge10", "detail":"thumb\_menehunemac.png", "phone":"8088413344", "url":"http://www.menehunemac.com/", "location": "707 Waiakamilo Road, Honolulu, Hawaii 96817", "latitude":"21.292100", "longitude":"-157.844500", "H1":"10% Off entire purchase when you mention Happy Haleiwa", "H2":"Limit one per customer" }]
The confusing part is that I actually released this app less than a year ago and it was working fine. I came back to make some changes and its giving me errors now!
Could someone help me understand what I’m doing wrong? Im running V 2012.971
I really appreciate the help!