Creating A JSON File ON Device

Hello.

    My current app is completely working in the Corona Simulator. However, when I build it for Android and run it on my testing device the app freezes at the loading screen (The only function is to load a json file). I assume that that is because there is no json file on the device. 

    Is this a correct assumption? If so I would need to try loading my json file and if it does not exist save a default table. How would I go about creating a new json file on the device that I can read and write to?

Thanks for the help

-Summit Tech

This thread explains what might be going on:  http://forums.coronalabs.com/topic/28066-no-resource-folder-in-android/

But a .json file should be a file that’s getting extracted as long as you’re building a path to with with system.pathToFile()…

Files you write and save are to the system.DocumentsDirectory folder and you shouldn’t run into issues writing a .json file there (and then reading it back in)

Thanks. If I wanted to build my app with a premade json file, how would I do it? I tried searching my computer for a json file I made but couldn’t find it.

-Summit Tech

JSON files are pretty similar to Lua table syntax actually.   JSON is just a plain text file with a particular format.  See:  http://json.org/example.html for examples and http://json.org/ to learn more.

You can use whatever text editor you use and save the file to the same folder as your main.lua

Rob

Hmm… Strange. I created a file called mygamesettings.json and put it in the same folder as my main.lua. Now when I update the file on the simulator nothing changes in the file, even after saving it. When I do make a change I can print the value with the correct value. On my device the app just crashes, even with a json file compiled with everything.

If it’s in system.ResourcesDirectory, it’s read only.  If you want to be able to update it, you will have to copy it to system.DocumentsDirectory and then read/write from it there.  If this is just a Lua table, you can use json_encode(tablename) and write out the resulting string.  This is what I used in my table saving functions:

http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

Rob

Hey Rob. For this app I’m pretty much using your code line for line. Everything works in rhe simulator. However I cannot find my json file. Where would I find it? I made a file called mygamesettings.json with my desire values but it seemed to not be using that file.

Ok, so I am using abd to debug on my phone. My print functions are telling me every time I start the app loadTable cannot find my requested file, so then I use saveTable to create the file which is missing. I ask it to print its path. I get “/data/data/com.games.summittech.Bubble_Pop/app_data/mygamesettings.json”. Does this location affect how the data is written or saved?

In the simulator, you can do a File->Show Project Sandbox.   That should open up a file system window to the folder where your files are.  You will see folders for all your apps and the selected one should be highlighted.  Open that up and you should see a Documents and tmp folder.  Documents is where your system.DocumentsDirectory is pointing and in that folder you should see your saved file.

Now on your device, (I’m assuming Android), unless it’s rooted, you can’t actually see the app’s sandbox files.  But that path sounds like the right path to the files. 

When your app starts, you should try and do a loadTable() :

local mySettings = loadTable(“mygamesettings.json”)

if mySettings == nil then

     mySettings = {}

     mySettings.soundOn = true

     – etc.  whatever reasonable starting values you need.

     saveTable(mySettings, “mygamesettings.json”)

end

The next time the app starts, it should find the file and load in the current settings.  Any time you change a setting, just call the saveTable() function.  Since it’s in memory, you only need to read it on app start up.

Rob

Thanks Rob. After a bit of looking around I created mygamesettings.json.txt and saved that.

This thread explains what might be going on:  http://forums.coronalabs.com/topic/28066-no-resource-folder-in-android/

But a .json file should be a file that’s getting extracted as long as you’re building a path to with with system.pathToFile()…

Files you write and save are to the system.DocumentsDirectory folder and you shouldn’t run into issues writing a .json file there (and then reading it back in)

Thanks. If I wanted to build my app with a premade json file, how would I do it? I tried searching my computer for a json file I made but couldn’t find it.

-Summit Tech

JSON files are pretty similar to Lua table syntax actually.   JSON is just a plain text file with a particular format.  See:  http://json.org/example.html for examples and http://json.org/ to learn more.

You can use whatever text editor you use and save the file to the same folder as your main.lua

Rob

Hmm… Strange. I created a file called mygamesettings.json and put it in the same folder as my main.lua. Now when I update the file on the simulator nothing changes in the file, even after saving it. When I do make a change I can print the value with the correct value. On my device the app just crashes, even with a json file compiled with everything.

If it’s in system.ResourcesDirectory, it’s read only.  If you want to be able to update it, you will have to copy it to system.DocumentsDirectory and then read/write from it there.  If this is just a Lua table, you can use json_encode(tablename) and write out the resulting string.  This is what I used in my table saving functions:

http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

Rob

Hey Rob. For this app I’m pretty much using your code line for line. Everything works in rhe simulator. However I cannot find my json file. Where would I find it? I made a file called mygamesettings.json with my desire values but it seemed to not be using that file.

Ok, so I am using abd to debug on my phone. My print functions are telling me every time I start the app loadTable cannot find my requested file, so then I use saveTable to create the file which is missing. I ask it to print its path. I get “/data/data/com.games.summittech.Bubble_Pop/app_data/mygamesettings.json”. Does this location affect how the data is written or saved?

In the simulator, you can do a File->Show Project Sandbox.   That should open up a file system window to the folder where your files are.  You will see folders for all your apps and the selected one should be highlighted.  Open that up and you should see a Documents and tmp folder.  Documents is where your system.DocumentsDirectory is pointing and in that folder you should see your saved file.

Now on your device, (I’m assuming Android), unless it’s rooted, you can’t actually see the app’s sandbox files.  But that path sounds like the right path to the files. 

When your app starts, you should try and do a loadTable() :

local mySettings = loadTable(“mygamesettings.json”)

if mySettings == nil then

     mySettings = {}

     mySettings.soundOn = true

     – etc.  whatever reasonable starting values you need.

     saveTable(mySettings, “mygamesettings.json”)

end

The next time the app starts, it should find the file and load in the current settings.  Any time you change a setting, just call the saveTable() function.  Since it’s in memory, you only need to read it on app start up.

Rob

Thanks Rob. After a bit of looking around I created mygamesettings.json.txt and saved that.