This tutorial will show you how we can map JSON string to a list of POJO objects and a list of POJO objects to JSON string using Google Gson API. For this tutorial we will create a standalone maven project in Eclipse.
If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.

Prerequisites

The following things are required in order to run the application

Eclipse Helios
JDK 1.6
Have maven installed and configured
Gson dependency in pom.xml

Now we will see the below steps how to create a maven based project in Eclipse

Step 1. Create a standalone maven project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as maven-archtype-quickstart from the below list of archtypes and click on Next button.

Now enter the required fields (Group Id, Artifact Id) as shown below

Group Id : com.roytuts
Artifact Id: jsontest

Step 2. Modify the pom.xml file as shown below.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.roytuts</groupId>
	<artifactId>jsontest</artifactId>
	<packaging>jar</packaging>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<jdk.version>1.6</jdk.version>
		<gson.version>2.3.1</gson.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>${gson.version}</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>jsontest</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>${jdk.version}</source>
					<target>${jdk.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Step 3. If you see JRE System Library[J2SE-1.4] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.4], click on Edit button and select the appropriate jdk 1.6 from the next window. Click on Finish then Ok.

Step 4. Create a Java package under src/main/java folder – com.roytuts.json.dto.test.

Step 5. Create an Employee object under the above created package with below source code.

package com.roytuts.json.dto.test;

public class Employee {

  private int id;
  private String name;
  private String email;

  public Employee() {

  }

  public Employee(int id, String name, String email) {
    this.id = id;
    this.name = name;
    this.email = email;
  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  @Override
  public String toString() {
    return "Employee ID : " + id + ", Name : " + name + ", Email : " + email;
  }

}

Step 6. Create a main Java class to map JSON string to a list of POJO objects.

package com.roytuts.json.dto.test;

import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class JsonDTOTest {

  private static final String jsonEmp = "[{\"id\":1001,\"name\":\"Sumit\",\"email\":\"sumit.ghosh@email.com\"}, {\"id\":1002,\"name\":\"Soumitra\",\"email\":\"soumitra.sarkar@email.com\"}, {\"id\":1003,\"name\":\"Gourab\",\"email\":\"gourab.guha@email.com\"}, {\"id\":1004,\"name\":\"Debina\",\"email\":\"debina.guha@email.com\"}, {\"id\":1005,\"name\":\"Debabrata\",\"email\":\"debabrata.poddar@email.com\"}, {\"id\":1006,\"name\":\"Liton\",\"email\":\"liton.sarkar@email.com\"}, {\"id\":1007,\"name\":\"Souvik\",\"email\":\"souvik.sanyal@email.com\"}]";

  public static void main(String[] args) {

    Gson gson = new Gson();

    //using array
    Employee[] employeeArray = gson.fromJson(jsonEmp, Employee[].class);
    System.out.println("JSON to List of POJOs using array");
    for (int i = 0; i < employeeArray.length; i++) {
      System.out.println(employeeArray[i]);
    }

  }
}

Step 7. Run the above class. You will get the below output in the console.

JSON to List of POJOs using array
Employee ID : 1001, Name : Sumit, Email : sumit.ghosh@email.com
Employee ID : 1002, Name : Soumitra, Email : soumitra.sarkar@email.com
Employee ID : 1003, Name : Gourab, Email : gourab.guha@email.com
Employee ID : 1004, Name : Debina, Email : debina.guha@email.com
Employee ID : 1005, Name : Debabrata, Email : debabrata.poddar@email.com
Employee ID : 1006, Name : Liton, Email : liton.sarkar@email.com
Employee ID : 1007, Name : Souvik, Email : souvik.sanyal@email.com

You can also map JSON string to a list of POJO objects using the following way.

package com.roytuts.json.dto.test;

import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class JsonDTOTest {

  private static final String jsonEmp = "[{\"id\":1001,\"name\":\"Sumit\",\"email\":\"sumit.ghosh@email.com\"}, {\"id\":1002,\"name\":\"Soumitra\",\"email\":\"soumitra.sarkar@email.com\"}, {\"id\":1003,\"name\":\"Gourab\",\"email\":\"gourab.guha@email.com\"}, {\"id\":1004,\"name\":\"Debina\",\"email\":\"debina.guha@email.com\"}, {\"id\":1005,\"name\":\"Debabrata\",\"email\":\"debabrata.poddar@email.com\"}, {\"id\":1006,\"name\":\"Liton\",\"email\":\"liton.sarkar@email.com\"}, {\"id\":1007,\"name\":\"Souvik\",\"email\":\"souvik.sanyal@email.com\"}]";

  public static void main(String[] args) {

    Gson gson = new Gson();

    System.out.println();
    //using collection
    Type collectionType = new TypeToken<List<Employee>>() {}.getType();
    List<Employee> employeeList = gson.fromJson(jsonEmp, collectionType);
    System.out.println("JSON to List of POJOs using collection");
    for (Employee employee : employeeList) {
      System.out.println(employee);
    }
	
  }
}

Output

JSON to List of POJOs using collection
Employee ID : 1001, Name : Sumit, Email : sumit.ghosh@email.com
Employee ID : 1002, Name : Soumitra, Email : soumitra.sarkar@email.com
Employee ID : 1003, Name : Gourab, Email : gourab.guha@email.com
Employee ID : 1004, Name : Debina, Email : debina.guha@email.com
Employee ID : 1005, Name : Debabrata, Email : debabrata.poddar@email.com
Employee ID : 1006, Name : Liton, Email : liton.sarkar@email.com
Employee ID : 1007, Name : Souvik, Email : souvik.sanyal@email.com

Step 8. Create below code snippets to map a list of POJO objects to a JSON string.

package com.roytuts.json.dto.test;

import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class JsonDTOTest {

  private static final String jsonEmp = "[{\"id\":1001,\"name\":\"Sumit\",\"email\":\"sumit.ghosh@email.com\"}, {\"id\":1002,\"name\":\"Soumitra\",\"email\":\"soumitra.sarkar@email.com\"}, {\"id\":1003,\"name\":\"Gourab\",\"email\":\"gourab.guha@email.com\"}, {\"id\":1004,\"name\":\"Debina\",\"email\":\"debina.guha@email.com\"}, {\"id\":1005,\"name\":\"Debabrata\",\"email\":\"debabrata.poddar@email.com\"}, {\"id\":1006,\"name\":\"Liton\",\"email\":\"liton.sarkar@email.com\"}, {\"id\":1007,\"name\":\"Souvik\",\"email\":\"souvik.sanyal@email.com\"}]";

  public static void main(String[] args) {

    Gson gson = new Gson();

    //using collection
    Type collectionType = new TypeToken<List<Employee>>() {}.getType();
    List<Employee> employeeList = gson.fromJson(jsonEmp, collectionType);

    System.out.println();
    String jsonOutput = gson.toJson(employeeList, collectionType);
    System.out.println("JSON from List<Employee>");
    System.out.println(jsonOutput);
  }
}

Step 9. Run the above class and see the below output in the console.

JSON from List<Employee>
[{"id":1001,"name":"Sumit","email":"sumit.ghosh@email.com"},{"id":1002,"name":"Soumitra","email":"soumitra.sarkar@email.com"},{"id":1003,"name":"Gourab","email":"gourab.guha@email.com"},{"id":1004,"name":"Debina","email":"debina.guha@email.com"},{"id":1005,"name":"Debabrata","email":"debabrata.poddar@email.com"},{"id":1006,"name":"Liton","email":"liton.sarkar@email.com"},{"id":1007,"name":"Souvik","email":"souvik.sanyal@email.com"}]

That’s all. Thanks for reading.

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials Twitter Facebook  Google Plus Linkedin Or Email Me

Leave a Reply

Your email address will not be published. Required fields are marked *