Much like their web-based kin, mobile applications rely on data to make them work. Most applications require calls to an api or other data source, to make themselves useful. Whether it be a call to an RSS feed in your favorite news application, a call to a private API to make your favorite online service app work, or just a database connection to run your favorite MMO game, data is crucial to the mobile application.
When you deal with data pulled from an online source, it usually comes in one of two formats, either XML or JSON. Today we’re going to look at how to deal with JSON, and I’m going to introduce you to some tools I use to make the process as painless as possible.
Firstly, what is JSON? JSON stands for JavaScript Object Notation. It is a “lightweight data-interchange format” used transfer data between servers and client applications. JSON is touted as being XML’s low-fat brother, as it doesn’t use the sometimes bloated tag structure that XML does. Instead, data is organized into objects and arrays. Objects may contain arrays and vice-versa. Quite simply put, objects are unordered name: value pairs where the name and value are separated by a colon and the pair is separated by a comma. Arrays are denoted by brackets “[]“. For a complete explanation of JSON, including examples, check out either the wikipedia entry or the JSON.org website
Now that we know what JSON is, how do we use it? There are many ways to process JSON, including via third party libraries. However I prefer to keep things simple and handle the data using the built in java library code. Generally I make a call to a URL which returns the JSON string via an ASYNCTask (gotta keep that URL processing out of the main UI thread). I then create a JSONObject from that string and parse on the fly using a combination of .getJSONArray(), .getJSONObject(); and custom getObject() methods available through the default org.JSON.* libraries.
While writing the code for processing JSON on the fly, I find a few tools out there helpful:
JSON Lint (http://www.jsonlint.com/)
This tool simply validates a given JSON string. If I’m dealing with a small snippet of JSON that I have to process, I’ll drop it into the given window, which will neatly validate and format the code. Reading unformatted JSON outputs is a pain; don’t do it. Use a tool such as this to clean it up, so you can see what’s going on.
Json Editor (http://jsoneditor.net/)
When the JSON gets a little more complicated, pull out this tool. JSON editor lets you see the data in a tree format, which is useful when writing code to parse the JSON output on the fly. In addition, its a Json Editor, meaning that, if necessary, you can write up your own edits to the JSON and get that back as a revised JSON file.
Developers, what are you favorite tools for dealing with JSON? Do you use IDE plugins? Online site? Emacs? Let us know in the comments below or via twitter @droidweb


