A question on JSON files

Hey everyone, I’m reading through the JSON info http://coronalabs.com/blog/2011/08/03/tutorial-exploring-json-usage-in-corona/ but I’m wondering, if I have a JSON file that’s distributed with the game initially (in system.resourceDirectory) do I need to, at some point save it as a regular file to system.documentDirectory through the regular IO structure? or is it possible to set a destination while encoding?

Hi @aku8,

I’m not sure what you’re asking. When you encode a Lua table into JSON, it returns the JSON string. What you do with it is your decision. If you want to save it to the file system, then you can, but you can’t write to the Resource directory, of course. The encode function doesn’t actually “write” anything, it merely returns the Lua table in JSON format.

Hope this helps,

Brent

I’m having a seemingly (to me) strange problem with my json. I’m not getting any errors reading the file (and I’m checking to make sure the file is there), however, whenever I go to get the size of the json list, it’s coming back as empty. Here’s my current temp function reading the json:

function checkLives() if(File:doesFileExist("Data/user.json")) then --stuff to do if the result is true. Read the json file, get time of last play, number of lives, and max lives print( "True" ) local path = system.pathForFile( "Data/user.json",system.ResourceDirectory ) local file = io.open( path,"r" ) if(file) then print( "The file works" ) contents = file:read("\*a") gameSettings = json.decode( contents ) print(#gameSettings) end

I was first trying to read it from the documentsDirectory,  however to ensure that I wasn’t making an error there, I switched back to reading directly from the base distribution file, and I’m still getting a 0 for the print() Any ideas where I’m going wrong?

This is my JSON read function based on sample data like this.

{"section1":{"value1":1,"value2":2}, "section2":{"value1":3,"value2":4}} 

Code to process this (loaded from file)

local function ReadSavedJSONFile() local dataIsBad = false -- Get Contents of File local path = system.pathForFile( "latest.json", system.DocumentsDirectory ) local file = io.open( path, "r" ) local savedData = file:read( "\*a" ) io.close( file ) if tonumber(string.find(savedData, "Error:")) ~= nil then if tonumber(string.find(savedData, "Error:")) \> 0 then dataIsBad = true else -- debug only native.showAlert( "Debug", "No json data was found." , {"OK"}, onGenericAlertBox) end end if dataIsBad == true then -- do something to recover below PreferencesDelete() PreferencesSaveDefault() PreferencesLoad() else -- data ok, read \_G.latestjsondata = json.decode( jsonFileInDocs( "latest.json" ) ) print("Debug JSON DATA: ") print("section1.value1: " .. tostring(\_G.latestjsondata.section1.value1)) print("section1.value2: " .. tostring(\_G.latestjsondata.section1.value2))

You can then nest values as much as you want but make sure you read back that section with sub sections.

tostring(\_G.latestjsondata.mainsection.subsection1.subsection2.subsection3.subsection4.value)

P.S “_G”'s are bad so use local vars.

Hi @aku8,

I’m not sure what you’re asking. When you encode a Lua table into JSON, it returns the JSON string. What you do with it is your decision. If you want to save it to the file system, then you can, but you can’t write to the Resource directory, of course. The encode function doesn’t actually “write” anything, it merely returns the Lua table in JSON format.

Hope this helps,

Brent

I’m having a seemingly (to me) strange problem with my json. I’m not getting any errors reading the file (and I’m checking to make sure the file is there), however, whenever I go to get the size of the json list, it’s coming back as empty. Here’s my current temp function reading the json:

function checkLives() if(File:doesFileExist("Data/user.json")) then --stuff to do if the result is true. Read the json file, get time of last play, number of lives, and max lives print( "True" ) local path = system.pathForFile( "Data/user.json",system.ResourceDirectory ) local file = io.open( path,"r" ) if(file) then print( "The file works" ) contents = file:read("\*a") gameSettings = json.decode( contents ) print(#gameSettings) end

I was first trying to read it from the documentsDirectory,  however to ensure that I wasn’t making an error there, I switched back to reading directly from the base distribution file, and I’m still getting a 0 for the print() Any ideas where I’m going wrong?

This is my JSON read function based on sample data like this.

{"section1":{"value1":1,"value2":2}, "section2":{"value1":3,"value2":4}} 

Code to process this (loaded from file)

local function ReadSavedJSONFile() local dataIsBad = false -- Get Contents of File local path = system.pathForFile( "latest.json", system.DocumentsDirectory ) local file = io.open( path, "r" ) local savedData = file:read( "\*a" ) io.close( file ) if tonumber(string.find(savedData, "Error:")) ~= nil then if tonumber(string.find(savedData, "Error:")) \> 0 then dataIsBad = true else -- debug only native.showAlert( "Debug", "No json data was found." , {"OK"}, onGenericAlertBox) end end if dataIsBad == true then -- do something to recover below PreferencesDelete() PreferencesSaveDefault() PreferencesLoad() else -- data ok, read \_G.latestjsondata = json.decode( jsonFileInDocs( "latest.json" ) ) print("Debug JSON DATA: ") print("section1.value1: " .. tostring(\_G.latestjsondata.section1.value1)) print("section1.value2: " .. tostring(\_G.latestjsondata.section1.value2))

You can then nest values as much as you want but make sure you read back that section with sub sections.

tostring(\_G.latestjsondata.mainsection.subsection1.subsection2.subsection3.subsection4.value)

P.S “_G”'s are bad so use local vars.