Gson – Convert JSON array to List example shows how to convert JSON array to a Java List using the Gson library in Java. This example also shows how to convert a JSON array to list using the fromJson method of the Gson class.
How to convert a JSON array to a Java List?
Many times the JSON contains an array that we want to convert to a Java List such as an ArrayList or a LinkedList object. This can be achieved using the fromJson
method of the Gson class.
1 2 3 |
public <T> T fromJson(java.lang.String json, java.lang.Class<T> classOfT) throws JsonSyntaxException |
This fromJson
method deserializes the given JSON data into an object of the specified type. Consider below given JSON data.
1 2 3 4 5 6 |
{ "id": "EMP001", "name": "Bob", "age": 22, "languages": ["English", "Spanish"] } |
The employee JSON contains known languages as a JSON array. Let’s convert this to a Java list object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
package com.javacodeexamples.gsonexamples; import java.util.List; import com.google.gson.Gson; public class GsonJSONArrayToListExample { public static void main(String[] args) { String jsonData = "{\"id\": \"EMP001\",\"name\": \"Bob\",\"age\": 22,\"languages\": [\"English\", \"Spanish\"]}"; //create a new Gson object Gson gsonObject = new Gson(); /* * The fromJson method will automatically take * care of the conversion. */ Employee employee = gsonObject.fromJson(jsonData, Employee.class); //print language list for(String language : employee.getLanguages()) { System.out.println(language); } } } class Employee{ private String id; private String name; private int age; //this list will hold data of languages array private List<String> languages; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<String> getLanguages() { return languages; } public void setLanguages(List<String> languages) { this.languages = languages; } } |
Output
1 2 |
English Spanish |
We converted the language JSON array specified as a property value to a Java List object. How about converting a root-level JSON array to a List? Let’s see an example of that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[ { "id": "EMP001", "name": "Bob", "age": 22, "languages": ["English", "Spanish"] }, { "id": "EMP002", "name": "June", "age": 27, "languages": ["English", "French", "German"] }, { "id": "EMP003", "name": "Raj", "age": 32, "languages": ["English", "Hindi", "Tamil"] } ] |
Now we have a JSON array containing individual employee objects as JSON array elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
package com.javacodeexamples.gsonexamples; import java.lang.reflect.Type; import java.util.List; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class GsonJSONArrayToListExample { public static void main(String[] args) { String jsonData = "[" + "{\"id\": \"EMP001\",name\": \"Bob\",\"age\": 22,\"languages\": [\"English\", \"Spanish\"]}," + "{\"id\": \"EMP002\",\"name\": \"June\",\"age\": 27,\"languages\": [\"English\", \"French\", \"German\"]}," + "{\"id\": \"EMP003\",\"name\": \"Raj\",\"age\": 32,\"languages\": [\"English\", \"Hindi\", \"Tamil\"]}" + "]"; //create a new Gson object Gson gsonObject = new Gson(); /* * To convert JSON array to Java List, * create a type List<Employee> */ Type listType = new TypeToken<List<Employee>>(){}.getType(); //convert using fromJson method List<Employee> listEmployee = gsonObject.fromJson(jsonData, listType); //iterate list for(Employee employee : listEmployee) { System.out.println(employee.getId() + " => " + employee.getLanguages()); } } } class Employee{ private String id; private String name; private int age; //this list will hold data of languages array private List<String> languages; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<String> getLanguages() { return languages; } public void setLanguages(List<String> languages) { this.languages = languages; } } |
Output
1 2 3 |
EMP001 => [English, Spanish] EMP002 => [English, French, German] EMP003 => [English, Hindi, Tamil] |
Instead of using the Type, we can also use the toList
method of the Arrays class to do the same conversion as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
String jsonData = "[" + "{\"id\": \"EMP001\",name\": \"Bob\",\"age\": 22,\"languages\": [\"English\", \"Spanish\"]}," + "{\"id\": \"EMP002\",\"name\": \"June\",\"age\": 27,\"languages\": [\"English\", \"French\", \"German\"]}," + "{\"id\": \"EMP003\",\"name\": \"Raj\",\"age\": 32,\"languages\": [\"English\", \"Hindi\", \"Tamil\"]}" + "]"; //create a new Gson object Gson gsonObject = new Gson(); //convert using fromJson method List<Employee> listEmployee = Arrays.asList( gsonObject.fromJson(jsonData, Employee[].class) ); //iterate list for(Employee employee : listEmployee) { System.out.println(employee.getId() + " => " + employee.getLanguages()); } |
Output
1 2 3 |
EMP001 => [English, Spanish] EMP002 => [English, French, German] EMP003 => [English, Hindi, Tamil] |
Important Note: The list object returned by the asList
method of the Arrays class returns a fixed-sized list backed by the original array, so we can not add or remove elements from it.
However, we can always convert the List to an ArrayList as given below.
1 2 3 |
List<Employee> listEmployee = new ArrayList<Employee>( Arrays.asList( gsonObject.fromJson(jsonData, Employee[].class) ) ); |
Please let me know your views in the comments section below.