- Because XML tags on the same level can have the same name, accessing XML data from Corona can get to be really confusing if you don’t pre-localize all the children in each level of the node hierarchy with your own naming convention (see? very confusing).
- It’s really only useful if you need to read-in data from an XML file. If you need to package up data into an XML file from your Corona app, you’re in for a rough ride.
Thankfully, using JSON in a Corona app is a lot more practical, and this time, there’s no external module you need to download, because JSON functions are now integrated into the Corona core!
Here’s a file, sample.json, that we’ll be using throughout this tutorial:
NOTE: As you can see from the sample above, the main difference between JSON and a Lua table is the use of colons “:” instead of the equals sign “=” for assignment.
If your JSON is stored in a .json file, as with sample.json, then you’ll first need to read the file into a variable. Here’s a function that’ll help you load an external json file and store it in a variable as a string:
NOTE: If you decide to put the above function in an external module, be sure to remove “local” from line 2.
Decoding the JSON
When you have a JSON string, you need to “decode” it, which is another way of saying, “take my JSON and convert it into a Lua table so I can use it”.
Here’s how to use the function I provided above to load a (local) JSON file and
“decode” it into a Lua table:
local json = require "json"
local t = json.decode( jsonFile( "sample.json" ) )
The first line will make Corona’s JSON functions available via the “json” namespace. Visit the JSON documentation page to see all of Corona’s JSON functions.
The next line will call json.decode() and load the local JSON file using the function I provided earlier. The json.decode() function takes one argument, which is a JSON-encoded string.
Here is the contents of table t from the example above:
Now you can access the JSON data in the exact way as you access data with any other Lua table! See? Much easier than XML.
Encoding an Existing Table
What if you have an existing Lua table and you need to turn it into a JSON string? This is also known as “serializing” a table, and can be done with just one line of code:
local jsonString = json.encode( myLuaTable )
This is useful if you want to either take the string and save it into a file for later loading (e.g. “decoding”), or if you need to send a lot of data over the web and want to do it as easy as possible.
To work with remote JSON files, the process is exactly the same, except that you’ll need to download the file first.
Because JSON itself is very similar to Lua tables, and the fact that we’ve integrated the JSON library into the Corona core for easier access makes it obvious that we recommend using JSON over XML, but there may be cases where you still need to use XML.
For more information on Corona’s integrated JSON functions, see the JSON Library documentation.
Or, go read about using XML in Corona if that’s what your project calls for.
Got questions or have something to say? Sound off in the comments section!