Serialize/ convert list of objects to/from json in java (Gson & example)

By | February 2, 2016
  • Given an list of user defined objects, we would like to convert list of pojo objects to JSON (and JSON to list of objects).
  • We will use the Google gson library to achieve the conversion.
  • We will create Person class and we will perform the following operations with Person class
    • Convert List of Person objects to JSON.
    • Convert the JSON to List of Person objects

GSON maven dependency

<dependency>
	<groupId>com.google.code.gson</groupId>
	<artifactId>gson</artifactId>
	<version>2.5</version>
</dependency>

Program – convert list of objects to/from json in java (GSON & example)

1.) Person Class:

  • Person class containing firstName, lastName, age etc.
  • We have overloaded toString method to print person information.
package org.learn.gson;

public class Person {
	public String firstName;
	public String lastName;
	public int age;
	public String contact;
	public Person(String firstName, String lastName,
				int age, String contact) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.age = age;
		this.contact = contact;
	}
	public String toString() {
	    return "[" + firstName + " " + lastName +
		       " " + age + " " +contact +"]";
	}
}

2.) JSONConverter Class:

JSONConverter is responsible for performing following operations.

  1. Convert List of Person objects to JSON.
  2. Convert the JSON to List of Person objects.
package org.learn.gson;

import java.lang.reflect.Type;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

public class JSONConverter 
{
    public static void main( String[] args )
    {
    	Gson objGson = new GsonBuilder().setPrettyPrinting().create();
    	    
    	List  personList = Stream.of(
				new Person("Mike", "harvey", 34, "001894536"),
			  	new Person("Nick", "young", 75,  "005425676"),
				new Person("Jack", "slater", 21 ,"009654153"),
				new Person("gary", "hudson", 55,"00564536"),
				new Person("Mike", "harvey", 21 ,"003685417"),
				new Person("gary", "hudson", 25,"00452341"))
				.collect(Collectors.toList());
    	
    	//Convert list to json
    	System.out.println("1. Convert list of person objects to Json");
    	String json = objGson.toJson(personList);
    	System.out.println(json);
    	
    	//Convert json back to list
    	System.out.println("2. Convert JSON to list of person objects");
    	Type listType = new TypeToken<List>() {}.getType();
    	List readFromJson = objGson.fromJson(json, listType);
    	readFromJson.forEach(System.out::println);
    }
}

Download Code – convert list of objects to/from json (GSON)

Output – convert list of objects to/from json in java (GSON & example)


1. Convert list of person objects to Json
[
{
"firstName": "Mike",
"lastName": "harvey",
"age": 34,
"contact": "001894536"
},
{
"firstName": "Nick",
"lastName": "young",
"age": 75,
"contact": "005425676"
},
{
"firstName": "Jack",
"lastName": "slater",
"age": 21,
"contact": "009654153"
},
{
"firstName": "gary",
"lastName": "hudson",
"age": 55,
"contact": "00564536"
},
{
"firstName": "Mike",
"lastName": "harvey",
"age": 21,
"contact": "003685417"
},
{
"firstName": "gary",
"lastName": "hudson",
"age": 25,
"contact": "00452341"
}
]
2. Convert JSON to list of person objects
[Mike harvey 34 001894536]
[Nick young 75 005425676]
[Jack slater 21 009654153]
[gary hudson 55 00564536]
[Mike harvey 21 003685417]
[gary hudson 25 00452341]