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.
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