0
\$\begingroup\$

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  
}  
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Use DialoguesTable = putJsonDataInTable() instead of putJsonDataInTable() \$\endgroup\$ Commented Feb 20, 2020 at 11:24
  • 1
    \$\begingroup\$ @EgorSkriptunoff please post that as an answer we can upvote/accept :) \$\endgroup\$ Commented Feb 25, 2020 at 18:23

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.