Hey, so here is my code.
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- local json = require ("json") display.setStatusBar( display.HiddenStatusBar ) background = display.newRect(0,-90,640,1136) title = display.newText( "File Demo", 0, 30, "Verdana-Bold", 20 ) title.x = display.contentWidth/2 -- center title title:setTextColor( 255,0,0 ) t={ name="Max", age="14" } local json = require("json") function saveTable(t, filename) local path = system.pathForFile( filename, system.DocumentsDirectory) local file = io.open(path, "w") if file then local contents = json.encode(t) file:write( contents ) io.close( file ) title:setTextColor( 255,255,0 ) root1 = display.newText( path, 0, 300, "Verdana-Bold", 5 ) root1.x = display.contentWidth/2-- center title root1:setTextColor( 0,0,255 ) return true else return false end end function loadTable(filename) path = system.pathForFile( filename, system.DocumentsDirectory) 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 ) title:setTextColor( 255,0,255 ) myTable.name = name1 testText = display.newText( name1, 300, 300, "Verdana-Bold", 5 ) return myTable end return nil end loadTable("test.json")
My saveTable function was a success.
On lines 56 and 57 you can see that I try to take a value from myTable and make it a string then try to print it, but it fails- saying “Line 57, bad argument, string expected, got nil”
Any help to this issue?