Read a element of a Json file

Hi, Im working in a app and need to save data. I use a Json file, where i save questions 

I do this:

local sceneName = …

local composer = require( “composer” )

local scene = composer.newScene( sceneName )

function scene:create (event)

local json = require( “json” )

local loadsave = require( “loadsave” )  

local filename = system.pathForFile( “file.json”, system.DocumentsDirectory)

local t = json.decodeFile(filename)

local preguntas ={

{preg1=“Color favorito?”},

{preg2=“Dia favorito?”}

}

local encode = json.encode(preguntas)

loadsave.saveTable( preguntas, “file.json” )

local file = io.open (filename, “r”)

contents = file:read("*a")

texto = display.newText(“leer” …contents.preg1 ,200 ,200)

end 

----- funciones ------

scene:addEventListener(“create”,scene)

I want to write preg1 in the screen but make a error.

Thanks!

You seem to be encoding the lua table ‘preguntas’ as a json string called ‘encode’, but do nothing with this.

You then save ‘preguntas’ as a json file using loadSave.saveTable.

You then read it back in as a raw string and store as ‘contents’. Therefore contents.preg1 does not exist.

I think you need to use loadSave.loadTable to retrieve the json table from file.json and decode it into a lua table.

For saving and loading you can use these two functions:

local json = require("json") --saves a table in a json file with the provided filename local function saveGame(saveTable, filename)     local path = system.pathForFile(filename, system.DocumentsDirectory) local file = io.open(path, "w") if file then local contents = json.encode(saveTable) file:write( contents ) io.close( file ) return true else return false end end --loads a json file with the provided name and returns it as a table (if it exists) local function loadGame(filename) local path = system.pathForFile(filename, system.DocumentsDirectory) local contents = "" local loadTable = {} local file = io.open( path, "r" ) if file then local contents = file:read( "\*a" ) loadTable = json.decode(contents); io.close( file ) return loadTable end return nil end --load data local exampleTable = {1, 2, 3} saveGame(exampleTable, "yourFilename") --save data local exampleTable = loadGame("yourFilename")

Hope that helps :slight_smile:

21:14:02.633 ERROR: Runtime error 21:14:02.633 D:\CORONA\_proj\IGRA\_ARA\menu.lua:79: attempt to call global 'saveGame' (a nil value) 21:14:02.633 stack traceback: 21:14:02.633 D:\CORONA\_proj\IGRA\_ARA\menu.lua:79: in function \<D:\CORONA\_proj\IGRA\_ARA\menu.lua:70\> 21:14:02.633 ?: in function 'dispatchEvent' 21:14:02.633 ?: in function \<?:865\> 21:14:02.633 (tail call): ? 21:14:02.633 ?: in function \<?:504\> 21:14:02.633 ?: in function \<?:169\>

Throws an error, screenshot attached

how to fix this?

21:14:02.633 D:\CORONA_proj\IGRA_ARA\menu.lua:79: attempt to call global ‘saveGame’ (a nil value)

Is the saveGame function defined above line 79? If not, it is out of scope and will result in an error. 

The two solutions are either to move the entire function somewhere above line 79, or define ‘local saveGame’ at the top of your lua file and then remove the ‘local’ at the point the saveGame function code resides.

You seem to be encoding the lua table ‘preguntas’ as a json string called ‘encode’, but do nothing with this.

You then save ‘preguntas’ as a json file using loadSave.saveTable.

You then read it back in as a raw string and store as ‘contents’. Therefore contents.preg1 does not exist.

I think you need to use loadSave.loadTable to retrieve the json table from file.json and decode it into a lua table.

For saving and loading you can use these two functions:

local json = require("json") --saves a table in a json file with the provided filename local function saveGame(saveTable, filename) &nbsp;&nbsp;&nbsp;&nbsp;local path = system.pathForFile(filename, system.DocumentsDirectory) local file = io.open(path, "w") if file then local contents = json.encode(saveTable) file:write( contents ) io.close( file ) return true else return false end end --loads a json file with the provided name and returns it as a table (if it exists) local function loadGame(filename) local path = system.pathForFile(filename, system.DocumentsDirectory) local contents = "" local loadTable = {} local file = io.open( path, "r" ) if file then local contents = file:read( "\*a" ) loadTable = json.decode(contents); io.close( file ) return loadTable end return nil end --load data local exampleTable = {1, 2, 3} saveGame(exampleTable, "yourFilename") --save data local exampleTable = loadGame("yourFilename")

Hope that helps :slight_smile:

21:14:02.633 ERROR: Runtime error 21:14:02.633 D:\CORONA\_proj\IGRA\_ARA\menu.lua:79: attempt to call global 'saveGame' (a nil value) 21:14:02.633 stack traceback: 21:14:02.633 D:\CORONA\_proj\IGRA\_ARA\menu.lua:79: in function \<D:\CORONA\_proj\IGRA\_ARA\menu.lua:70\> 21:14:02.633 ?: in function 'dispatchEvent' 21:14:02.633 ?: in function \<?:865\> 21:14:02.633 (tail call): ? 21:14:02.633 ?: in function \<?:504\> 21:14:02.633 ?: in function \<?:169\>

Throws an error, screenshot attached

how to fix this?

21:14:02.633 D:\CORONA_proj\IGRA_ARA\menu.lua:79: attempt to call global ‘saveGame’ (a nil value)

Is the saveGame function defined above line 79? If not, it is out of scope and will result in an error. 

The two solutions are either to move the entire function somewhere above line 79, or define ‘local saveGame’ at the top of your lua file and then remove the ‘local’ at the point the saveGame function code resides.