GSON – Parse JSON array to Java array or list

Learn to use Google GSON library to deserialize or parse JSON, containing json array, to java array or java list objects.

It’s worth mentioning that JSON has only array datatype. Java has both – arrays and lists.

1. Parse JSON array as root object

[
    {
      "name": "Alex",
      "id": 1
    },
    {
      "name": "Brian",
      "id": 2
    },
    {
      "name": "Charles",
      "id": 3
    }
]
public class User 
{
	private long id;
	private String name;
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + "]";
	}
}

1.1. Array of objects

Java program to deserialize json array as root – to Java array of objects.

String userJson = "[{'name': 'Alex','id': 1}, "
				+ "{'name': 'Brian','id':2}, "
				+ "{'name': 'Charles','id': 3}]";
		
Gson gson = new Gson(); 

User[] userArray = gson.fromJson(userJson, User[].class);  

for(User user : userArray) {
	System.out.println(user);
}

Program output.

User [id=1, name=Alex]
User [id=2, name=Brian]
User [id=3, name=Charles]

1.2. List of objects

Java program to deserialize json array as root – to Java list of objects.

import java.lang.reflect.Type;
import java.util.ArrayList;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

String userJson = "[{'name': 'Alex','id': 1}, "
				+ "{'name': 'Brian','id':2}, "
				+ "{'name': 'Charles','id': 3}]";
		
Gson gson = new Gson(); 

Type userListType = new TypeToken<ArrayList<User>>(){}.getType();

ArrayList<User> userArray = gson.fromJson(userJson, userListType);  

for(User user : userArray) {
	System.out.println(user);
}

Program output.

User [id=1, name=Alex]
User [id=2, name=Brian]
User [id=3, name=Charles]

2. Parse JSON array as member

Gson parses JSON arrays as members without difficulty if they are non-root objects. We can use the fromJson() method in usual manner and it will parse the json array correctly to required java array or list.

{
	"id" 	: 1,
	"name"	: "HR",
	"users" : [
	    {
	      "name": "Alex",
	      "id": 1
	    },
	    {
	      "name": "Brian",
	      "id": 2
	    },
	    {
	      "name": "Charles",
	      "id": 3
	    }
	]

2.1. Member Array

Java program to deserialize json array as member object – to Java array of objects as member field.

public class Department 
{
	private long id;
	private String name;
	private User[] users;

	//Getters and Setters

	@Override
	public String toString() {
		return "Department [id=" + id + ", name=" + name + ", users=" + Arrays.toString(users) + "]";
	}
}
String departmentJson = "{'id' : 1, "
		+ "'name': 'HR',"
		+ "'users' : ["
			+ "{'name': 'Alex','id': 1}, "
			+ "{'name': 'Brian','id':2}, "
			+ "{'name': 'Charles','id': 3}]}";

Gson gson = new Gson(); 

Department department = gson.fromJson(departmentJson, Department.class);  

System.out.println(department);

Program output.

Department [id=1, name=HR, 
	users=[User [id=1, name=Alex], 
		User [id=2, name=Brian], 
		User [id=3, name=Charles]]]

2.2. Member List

Java program to deserialize json array as member object – to Java list of objects a member field.

public class Department 
{
	private long id;
	private String name;
	private List<User> users;

	//Getters and Setters

	@Override
	public String toString() {
		return "Department [id=" + id + ", name=" + name + ", users=" + users + "]";
	}
}
String departmentJson = "{'id' : 1, "
		+ "'name': 'HR',"
		+ "'users' : ["
			+ "{'name': 'Alex','id': 1}, "
			+ "{'name': 'Brian','id':2}, "
			+ "{'name': 'Charles','id': 3}]}";

Gson gson = new Gson(); 

Department department = gson.fromJson(departmentJson, Department.class);  

System.out.println(department);

Program output.

Department [id=1, name=HR, 
	users=[User [id=1, name=Alex], 
		User [id=2, name=Brian], 
		User [id=3, name=Charles]]]

Drop me your question related to gson parse JSON array to java lists and arrays.

Happy Learning !!

References:

GSON Github

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

5 thoughts on “GSON – Parse JSON array to Java array or list”

  1. I’ve JSONArray response which contains other JSON array. Array of Posts and one post contains array of comments.Please help.

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.