Rapid JSON Parsing In Android

I have seen people spend a ton of time trying to figure out how to parse their API’s data into Plain Old Java Objects (POJO) so that they can use the data in their app. Using Google’s GSON and a handful of other free web tools you can parse just about any JSON data in five minutes. What follows is a step by step guide to completing this five minute process. The tutorial follows the process used to create the AndroidRapidGsonParsing; which is open source.

Step 1) Install the GSON Library

Download the GSON library; to install just drag the jar file into your libraries folder

Screen shot 2013-08-27 at 5.55.44 PM

Step 2) Find the API you want to get data from

Find an JSON API that you want to parse. Looking for a directory of APIs? I recommend ProgrammableWeb. For the purposes of this tutorial we are going to be parsing a list of streams available from Justin.tv using this documentation of the API. The API request to get the list of steams from Justin.tv in JSON format is: http://api.justin.tv/api/stream/list.json

Step 3) Call the API and capture some sample output

In order to proceed we need some sample data that is returned from the API. In this case we can just run the request in a web browser, and copy out the response such as:

[{"broadcast_part": 4, "featured": true, "channel_subscription": true, "audio_codec": "uncompressed", "id": "6640712464", "category": "gaming", "title": "Fnatic xPeke, Normals(ranked down) on smurf", "geo": "DE", "video_height": 1080, "site_count": 8014, "embed_enabled": true, "channel": {"subcategory": null, "producer": true, "image_url_huge": "http://static-cdn.jtvnw.net/jtv_user_pictures/xpeke-profile_image-a182a5fe5a8f239b-600x600.jpeg", "timezone": "Europe/Madrid", "screen_cap_url_huge": “http://static-...more data

Step 4) Generate the POJOs for your data

Now for the fun part, using the sample data we just captured we are going to auto generate some Plain Old Java Objects that represent that data. Use jsonschema2pojo and be sure to enter in your desired package name, class name, JSON as the type, and use primitive types. For this example the generator got confused because the root of the JSON is an array and not an object; so I only pasted in the first element of the array.

Screen shot 2013-08-27 at 6.06.56 PM

Step 5) Integrate the generated POJOs into your code

With the POJOs generated now we can just paste those generated classes into our project. In my sample project they are in the models package.

Step 6) Download the API data into a JSON String

The sample project contains a very simple downloader that you can use for testing purposes, but it doesn’t handle errors or support retries caching etc. How to do this is beyond the scope of the tutorial. I recommend you use either okHttp or Volley or a combination of the two.

Step 7) Use GSON to parse the JSON into POJOs

Once the JSON is in a String parsing is super easy. If the data starts with an JSON array such as this then to parse is as simple as:

Type collectionType = new TypeToken<List<JustinTvStreamData>>() {}.getType()
gson.fromJson(json, collectionType)

If your JSON begins with a JSON object than parsing is much easier and follows this format: gson.fromJson(data, JustinTvStreams.class)

For the full documentation on how to run the parser see here; this tutorial covers the two most common cases but there are others.

Step 8) Do something interesting with the data

Now that the data is in a POJO do something interesting with it!
For this demo project I display the Twitch.Tv channel image and description and use the Picasso Library to download and cache the images.


Does this sort of work sound interesting to you? We are looking for great Android Developers. Apply now!

blog comments powered by Disqus