How to parse json assets with gson

Published Mar 30, 2017

So most of us have json assets in our app which we parse on runtime to get the data and use it accordingly but what I have seen is that most of the people create a JSONObject or JSONArray after reading the json into an inputstream but then handling it becomes difficult since we have to manually extract every entity in each array which makes it bound to a lot of errors. A better approach to using it is making use of gson : an open source library by Google to serialise and deserialise Java object to (and from) Json. It’s pretty easy to use and makes the development process easy. For those of you not still convinced on using gson, I’d like to demonstrate the code we had to write to without using gson and the one using gson as well.
So to start with lets see the json file we’ll be using. It’s the events.json file from the open event project.

  "events": [
      "color": "#fdfdfd",
      "email": "",
      "end_time": "2015-07-14T00:00:00",
      "id": 4,
      "latitude": 37.783839,
      "location_name": "Moscone centre",
      "logo": "",
      "longitude": -122.400546,
      "name": "FOSSASIA",
      "slogan": "Fossasia",
      "start_time": "2015-05-28T13:00:00",
      "url": ""

As you can see it has an object that has an array of event objects. So what we’ll first do is that we’ll get the whole json as a string by openeing an inputstream and then directing it to a buffer. Then we convert the buffer array to a string object.

String json = null;
try {
    InputStream inputStream = getAssets().open("events.json");
    int size = inputStream.available();
    byte[] buffer = new byte[size];;
    json = new String(buffer, "UTF-8");

} catch (IOException e) {

Now we have the json as a string which we can now parse it using a combination of JSONObject and JSONArray. First we’ll access data in the outer json object i.e. “events”. That’ll be done by

JSONObject jsonObject = new JSONObject(json);
JSONArray events = jsonObject.getJSONArray("events");

Now that we have the array, we can traverse it to get the objects inside events array

for (int j=0; j < events.length(); j++){
    JSONObject cit = events.getJSONObject(j);
    String color = jsonObject.getString("color");
    String email = jsonObject.getString("email"); 
    String endTime = jsonObject.getString("end_time");
    String id = jsonObject.getString("id");
    String latitude = jsonObject.getString("latitude");
    String locationName = jsonObject.getString("location_name");
    String logo = jsonObject.getString("logo");
    String longitude = jsonObject.getString("longitude");
    String name = jsonObject.getString("name");
    String slogan = jsonObject.getString("slogan");
    String startTime = jsonObject.getString("start_time");
    String url = jsonObject.getString("url");

This is how we go about it. Now for the exiting part.
We already have an Event data class which has the constructor, getters and setters etc.

public class Event {

    int id;

    String name;

    String email;

    String color;

    String logo;

    String start;

    String end;

    float latitude;

    float longitude;

    String locationName;

    String url;

    String slogan;

    public Event(int id, String name, String email, String color, String logo, String start,
                 String end, float latitude, float longitude, String locationName, String url, String slogan) { = id; = name; = email;
        this.color = color;
        this.logo = logo;
        this.start = start;
        this.end = end;
        this.latitude = latitude;
        this.longitude = longitude;
        this.locationName = locationName;
        this.url = url;
        this.slogan = slogan;

    public String getEmail() {
        return email;

    public void setEmail(String email) { = email;

    public String getColor() {
        return color;

    public void setColor(String color) {
        this.color = color;

    public String getUrl() {
        return url;

    public void setUrl(String url) {
        this.url = url;

    public String getSlogan() {
        return slogan;

    public void setSlogan(String slogan) {
        this.slogan = slogan;

    public int getId() {

        return id;


    public void setId(int id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getLogo() {
        return logo;

    public void setLogo(String logo) {
        this.logo = logo;

    public String getStart() {
        return start;

    public void setStart(String start) {
        this.start = start;

    public String getEnd() {
        return end;

    public void setEnd(String end) {
        this.end = end;

    public float getLatitude() {
        return latitude;

    public void setLatitude(float latitude) {
        this.latitude = latitude;

    public float getLongitude() {
        return longitude;

    public void setLongitude(float longitude) {
        this.longitude = longitude;

    public String getLocationName() {
        return locationName;

    public void setLocationName(String locationName) {
        this.locationName = locationName;


Now here we name the parameters to the same as that in the json we have or we can just add @SerializedName(entity_name). Then we go to the code for actually retrieving the data from the json file using this data class. How we can do that is by first making a class that’ll get the array of events for us.

public class EventResponseList {
    public List<Event> event;

Now all we do is

EventResponseList eventResponseList = gson.fromJson(json, EventResponseList.class);

We have a list of events that were in the json array.
Voila! That’s it. It’s so easy to get a list of all the events from the JSONArray and since the library is available for gradle, it’s even better. You can just add
compile ''
to your build.gradle dependencies and you’re good to go. Cheers!

Discover and read more posts from Manan Wason
get started
Enjoy this post?

Leave a like and comment for Manan

Be the first to share your opinion

Get curated posts in your inbox

Read more posts to become a better developer