Problem statement:
Need help trying to load a JSON file into a table array in Lua for Corona SDK development.
My Objective:
Call specific data from JSON file to my application by using key value pairs.
Current Behavior of application
Corona simulator console just return a table ID like --> table: 114AF7D8
Expected Behavior
Returns the name "Smith" when i print(DialogsTable.name[2])
A reference to the Lua code:
local json = require( "json" )
local DialoguesTable = {}
local path = system.pathForFile("simple.json", system.DocumentsDirectory )
function putJsonDataInTable(filename, base)
local base = base or system.DocumentsDirectory
local path = system.pathForFile(filename, base)
local fh = io.open(path,"r")
if fh then
local contents = fh:read("*a")
io.close( fh )
local DialoguesTable = json.decode(contents)
return DialoguesTable
else
return nil
end
end
putJsonDataInTable()
print(DialoguesTable.name[2])
A reference to the JSON file:
{
"name":["John","Smith"],
"age":30
}
DialoguesTable = putJsonDataInTable()instead ofputJsonDataInTable()\$\endgroup\$