The JSON library now comes pre-installed

Posted by

The JSON library that we’ve been shipping in our sample code (e.g. Facebook) is now an official part of the Corona core. The really great thing about this library is that it makes serializing/deserializing Lua tables a no-brainer.

local json = require "json"

 -- Lua script:
 local t = {
    ["name1"] = "value1",
    ["name2"] = {1, false, true, 23.54, "a \021 string"},
    name3 = json.null()
 }

 local json = json.encode (t)
 print (json)
 --> {"name1":"value1","name3":null,"name2":[1,false,true,23.54,"a \u0015 string"]}

 local t = json.decode(json)
 print(t.name2[4])
 --> 23.54

As long as you don’t have userdata or functions or cycles (tables with child values that reference a parent table), you’re good to go.

For those of you who don’t speak SQL in your sleep or think the sqlite library is too heavyweight, it’s a great alternative that’s perfect for doing simple persistent storage.

Ready to get started?

Create amazing games and apps for iOS & Android

6 Comments

LeoJuly 1st, 2011 at 9:07 am

Nice!!

ShawnJuly 1st, 2011 at 9:18 am

Woo!

Maicol R.July 1st, 2011 at 9:26 am

help!

How to load external json?

local and external json files or json output…

tks.

Jonathan BeebeJuly 30th, 2011 at 6:57 am

@Maicol R: Stay tuned for an upcoming tutorial on using this integrated JSON library–to include using it in conjunction with external JSON text files.

Josep AlemanySeptember 10th, 2011 at 5:19 am

Very interesting!

Could you write a sample to use it with an http request???

Thanks in advance,

Josep Alemany

leoNovember 7th, 2011 at 2:00 am

How to use json with unicode

Leave a comment

Your comment