Gson – Parse JSON Array to Java Array or List

Learn to use Google GSON library to deserialize or parse a JSON array to a Java array or List object. It’s worth mentioning that JSON has only array datatype. Java has both – arrays and lists.

1. Parsing JSON Array as Root

To parse JSON, with array as root, we can use the following method syntax. Here ArrayItem is the class type of data elements in the array.

ArrayItem[] userArray = new Gson().fromJson(jsonSource, ArrayItem[].class); 

For converting such JSON array to a List, we need to use TypeToken class.

Type listType = new TypeToken<ArrayList<ArrayItem>>(){}.getType();
 
ArrayList<ArrayItem> list = gson.fromJson(jsonSource, listType);  

For demo purposes, we are using User.java as the JSON element type.

public class User
{
	private long id;
	private String name;
        
        // Getters and Setters
}

1.1. Converting JSON Array to 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. Converting JSON Array to List of Objects

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

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. Parsing 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 the usual manner and it will parse the JSON array correctly to the required java array or list.

2.1. Member JSON Array to Java 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
}
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 JSON Array to Java 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
}
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 !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode