JSON library that works

1 reply [Last post]
dwallin33
User offline. Last seen 18 weeks 1 day ago. Offline
Joined: 6 Feb 2010

I found some JSON code that works pretty well - http://www.chipmunkav.com/downloads/Json.lua

I tried the JSON4Lua library first, but for some reason it crashes on the device (not in the simulator..)

JSON is a nice way to serialize your objects for saving. For example:

function readScores()
local path = system.pathForFile( "scores.txt", system.DocumentsDirectory )
-- io.open opens a file at path. returns nil if no file found
local file = io.open( path, "r" )

if file then
-- read all contents of file into a string
local contents = file:read( "*a" )
scoresList = Json.Decode(contents);

io.close( file )

end
end

function saveScores()

local path = system.pathForFile( "scores.txt", system.DocumentsDirectory )

-- create file b/c it doesn't exist yet
local file = io.open( path, "w" )

if file then
file:write( Json.Encode(scoresList) )
io.close( file )
end

end

It is also useful for communicating with web-based services.

Replies

sirbeagle
User offline. Last seen 1 year 20 weeks ago. Offline
Joined: 12 Mar 2010

And I was just about to use JSON4Lua today too -- I'll start off with this other JSON code instead. Thanks for the tip dwallin3!

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.