JSON Parsing with GSON

JSON Basics

JSON is nowadays the standard way to transfer data between device and servers. JSON is very convenient because it is quite easy format to developers. It is also easy to read.

Here is example of simple JSON file:

{"obj1":100,"obj2":"abcd","obj3":false}

JSON data is a text string and always inside the curly brackets. Individual data is separated by commas. Data always includes key and value separated by semicolon. The key is inside quotation marks. If value is string, it is also inside quotation marks. If value is int, it is plain number. If value is boolean, it is written true or false.

GSON Basics

GSON is a nice library by Google to manage JSON files. You can write and read JSON files easily.

GSON can transfer the JSON file to a Java object. It also can write a JSON file from Java object.

Example files

In the example below, we have three files.

  • MainActivity.java where the actual code is,
  • Objects.java that is the Java object,
  • and activity_main.xml that is the layout file.

Activity_main.xml is not used in this example. It just shows the “Hello world!” text.

Objects.java

Objects.java is the class for Java object. It has three private variables: object1 is int, object2 is String and object3 is boolean. All those objects have getters.

Objects.java
// Class to keep JSON/GSON data
package com.example.gson;

class Objects {

    private int object1;
    private String object2;
    private boolean object3;

    int getObject1() {
        return object1;
    }

    String getObject2() {
        return object2;
    }

    boolean getObject3() {
        return object3;
    }
}

MainActivity.java

All the actual code is in MainActivity.java. In line 8 we import the Gson library. The code does not work without it.

MainActivity.java
// Class to read and write json file
package com.example.gson;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String json1 = "{\"obj1\":100,\"obj2\":\"abcd\",\"obj3\":false}";
        Log.v("json1", json1);

        // Deserialization (read from json file)
        Gson gson1 = new Gson();
        Objects jsonObjects = gson1.fromJson(json1, Objects.class);
        // Print objects
        Log.v("json1", "object1: " + jsonObjects.getObject1());
        Log.v("json1", "object2: " + jsonObjects.getObject2());
        Log.v("json1", "object3: " + jsonObjects.getObject3());

        // Serialization (write json file)
        Gson gson2 = new Gson();
        String json2 = gson2.toJson(jsonObjects);
        Log.v("json2", json2);
    }
}

In line 17, we define the JSON string. Because it is string, we need to put backlash in front of all quotation marks. Otherwise the code does not know where it starts and ends.

In lines 21-22, we read the JSON string and put its data to jsonObjects. In line 21, we create a new Gson object called gson1. In line 22, we create a new Object called jsonObjects. Then we read the json1 string and copy its content to jsonObjects.

Now in lines 24-26, we can print the contents of jsonObjects and check that it is same as contents of original json1.

After that we can create a new json string using the data from jsonObjects object in lines 29-30. In line 29, we create a new gson object called gson2. In line 30, we copy the contents of jsonObjects to json2. Finally we print it in line 31 and check that it is same as jsonObject.

Layout File activity_main.java

This layout file is not really used in this example. However it is needed by Android. The example does not work without it.

activity_main.java
// Layout file
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=“GSON!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>