Gson – Convert Java List to JSON array example shows how to convert a Java List to a JSON array using the Gson library. This example also shows how to do it using the toJson method of the Gson class.
How to convert Java List to JSON array?
We can use the toJson
method of the Gson class to convert a Java List to a JSON array.
1 |
public java.lang.String toJson(java.lang.Object src) |
This method serializes the specified object into the JSON representation.
Here is an example that converts a List of employee objects to an equivalent JSON representation.
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 |
package com.javacodeexamples.gsonexamples; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class GsonJavaListToJSONArrayExample { public static void main(String[] args) { List<Employee> listEmployees = new ArrayList<Employee>(); listEmployees.add( new Employee("EMP01", "Bob", 23, Arrays.asList("English", "French")) ); listEmployees.add( new Employee("EMP02", "May", 21, Arrays.asList("English", "German")) ); listEmployees.add( new Employee("EMP03", "Raj", 32, Arrays.asList("English", "Hindi")) ); /* * Use toJson method of the Gson class */ Gson gson = new GsonBuilder().setPrettyPrinting().create(); String json = gson.toJson(listEmployees); System.out.println(json); } } class Employee{ private String id; private String name; private int age; //this list will hold data of languages array private List<String> languages; public Employee(String id, String name, int age, List<String> languages) { this.id = id; this.name = name; this.age = age; this.languages = 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 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 |
[ { "id": "EMP01", "name": "Bob", "age": 23, "languages": [ "English", "French" ] }, { "id": "EMP02", "name": "May", "age": 21, "languages": [ "English", "German" ] }, { "id": "EMP03", "name": "Raj", "age": 32, "languages": [ "English", "Hindi" ] } ] |
As we can see from the output, each Java list element becomes a JSON array element.
If we want to convert a Java List to JsonElement, we can use the toJsonTree
method instead of the toJson
method.
1 |
public JsonElement toJsonTree(java.lang.Object src) |
This method serializes the specified object into a tree of JsonElement objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
List<Employee> listEmployees = new ArrayList<Employee>(); listEmployees.add( new Employee("EMP01", "Bob", 23, Arrays.asList("English", "French")) ); listEmployees.add( new Employee("EMP01", "May", 21, Arrays.asList("English", "German")) ); listEmployees.add( new Employee("EMP01", "Raj", 32, Arrays.asList("English", "Hindi")) ); /* * To convert a Java List object to a * tree of JsonElement object, use the toJsonTree method. */ JsonElement element = new Gson().toJsonTree(listEmployees); //get first employee object System.out.println(element.getAsJsonArray().get(0).getAsJsonObject()); //get first employee id System.out.println(element.getAsJsonArray().get(0).getAsJsonObject().get("id").getAsString()); |
Output
1 2 |
{"id":"EMP01","name":"Bob","age":23,"languages":["English","French"]} EMP01 |
Please let me know your views in the comments section below.