Intro To Android (Workbook 2)

Parsing

Once we have data, we need to do something with it!

A popular format is JSON and there are several parsing libraries you can use to turn your JSON data into usable Java objects. My favorite is GSON.

We can integrate this easily using maven:

<!--JSON parsing-->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.2.4</version>
</dependency>

You create a POJO structure that mirrors the data you're parsing and GSON takes care of the rest. For example, this is what my parking looks like for our example application:

Gson gson = new Gson();
LocationList list = gson.fromJson( result, LocationList.class );

It's simple if your structure matches exactly what you're downloading, but you can extend it to parse more complex data. In the example above, the LocationList is generated by the JSONSchema2Pojo tool referenced in this article for setting up JSON fetching (it uses Retrofit, but we will be using AsyncTask's with GSON parsing:

http://engineering.meetme.com/2014/03/best-practices-for-consuming-apis-on-android/