Json parsing not working on device on Corona

Json parsing is not working on devices while works perfect on simulator, that scene could not be open where I read or write some data. I have made a class for Json parsing Here it is,

module(…, package.seeall)

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)
return true
else
return false
end
end

function loadTable(filename)
local path = system.pathForFile(filename, system.DocumentsDirectory)
local contents = “”
local myTable = {}
local file = io.open(path,“r”)
if file then
local contents = file:read("*a")
myTable = json.decode(contents)
io.close(file)
return myTable
end
return nil
end

Any help?

This code should work so specify exacly what you do, what should happen and what really happens.

PS

module(…) is in the newest Lua specification depreciated. And while Corona still supports it, it’s strongly recommend not to use it.

How can I store data, for example the progress of a game?

You have functions to store tables so writr what are you asking precisely.
If you ask about my ps then search article ‘goodbye globals’ on google - it’S probably on corona labs blog

Here’s the link to the “Goodbye Globals!” tutorial:

http://www.coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

And for future reference, that is just one of many tutorials and guides in Corona University:

http://www.coronalabs.com/resources/tutorials/getting-started-with-corona/

Sincerely,

Brent Sorrentino

The #1 reason why things work in the sim but not on device has to do with filenames being case sensitive on the device.  However, everyone who can help you is blind until you can tell us what errors are happening on the device.  If you do not know how to read your device’s console log, please read this blog post.  Once we see what errors you are getting, in addition to how you are calling the saveTable/loadTable functions then we can possibly help you.

http://www.coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

Thanks

Rob

I would suggest looking at the contents of the file (on the device). I thought I was experiencing the same problem but I finally figured out that I needed to have a logged in browser session to download the json file. The simulator was using my desktop’s Safari session and was working perfectly. Also, no errors were being thrown on the device, it was just hanging up on the decode() function. The symptoms sound similar. 

Thank you very much everybody.

Thanks to “Tutorial Basic Debugging” I could solve the problem.

Joan

This code should work so specify exacly what you do, what should happen and what really happens.

PS

module(…) is in the newest Lua specification depreciated. And while Corona still supports it, it’s strongly recommend not to use it.

How can I store data, for example the progress of a game?

You have functions to store tables so writr what are you asking precisely.
If you ask about my ps then search article ‘goodbye globals’ on google - it’S probably on corona labs blog

Here’s the link to the “Goodbye Globals!” tutorial:

http://www.coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

And for future reference, that is just one of many tutorials and guides in Corona University:

http://www.coronalabs.com/resources/tutorials/getting-started-with-corona/

Sincerely,

Brent Sorrentino

The #1 reason why things work in the sim but not on device has to do with filenames being case sensitive on the device.  However, everyone who can help you is blind until you can tell us what errors are happening on the device.  If you do not know how to read your device’s console log, please read this blog post.  Once we see what errors you are getting, in addition to how you are calling the saveTable/loadTable functions then we can possibly help you.

http://www.coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

Thanks

Rob

I would suggest looking at the contents of the file (on the device). I thought I was experiencing the same problem but I finally figured out that I needed to have a logged in browser session to download the json file. The simulator was using my desktop’s Safari session and was working perfectly. Also, no errors were being thrown on the device, it was just hanging up on the decode() function. The symptoms sound similar. 

Thank you very much everybody.

Thanks to “Tutorial Basic Debugging” I could solve the problem.

Joan