jsonFile() is some function someone wrote to read a file in from the operating system and store it into your local memory. It’s not an official API call.
Look at something like this:
local function loadTable(filename) local path = system.pathForFile( filename, system.DocumentsDirectory) local contents = "" local myTable = {} local file = io.open( path, "r" ) if file then --print("trying to read ", filename) -- read all contents of file into a string local contents = file:read( "\*a" ) myTable = json.decode(contents); io.close( file ) --print("Loaded file") return myTable end print(filename, "file not found") return nil end
I know exactly the “jsonFile” function you’re talking about, but can’t find a reference to it now. The copy I have in my code looks like this:
function jsonFile( filename, base ) -- set default base dir if none specified if not base then base = system.ResourceDirectory; end -- create a file path for corona i/o local path = system.pathForFile( filename, base ) print( "jsonFile ["..path.."]" ) -- will hold contents of file local contents -- io.open opens a file at path. returns nil if no file found local file = io.open( path, "r" ) if file then -- read all contents of file into a string contents = file:read( "\*a" ) io.close( file ) -- close the file after using it else print( "\*\* Error: cannot open file" ) end print( "contents\<\<\<EOF") print( contents ) print( "\>\>EOF") return contents end
jsonFile() is some function someone wrote to read a file in from the operating system and store it into your local memory. It’s not an official API call.
Look at something like this:
local function loadTable(filename) local path = system.pathForFile( filename, system.DocumentsDirectory) local contents = "" local myTable = {} local file = io.open( path, "r" ) if file then --print("trying to read ", filename) -- read all contents of file into a string local contents = file:read( "\*a" ) myTable = json.decode(contents); io.close( file ) --print("Loaded file") return myTable end print(filename, "file not found") return nil end
I know exactly the “jsonFile” function you’re talking about, but can’t find a reference to it now. The copy I have in my code looks like this:
function jsonFile( filename, base ) -- set default base dir if none specified if not base then base = system.ResourceDirectory; end -- create a file path for corona i/o local path = system.pathForFile( filename, base ) print( "jsonFile ["..path.."]" ) -- will hold contents of file local contents -- io.open opens a file at path. returns nil if no file found local file = io.open( path, "r" ) if file then -- read all contents of file into a string contents = file:read( "\*a" ) io.close( file ) -- close the file after using it else print( "\*\* Error: cannot open file" ) end print( "contents\<\<\<EOF") print( contents ) print( "\>\>EOF") return contents end