Gson – convert JSON to a typed ArrayList example shows how to convert JSON to a type ArrayList, or an ArrayList of custom objects using the Gson library in Java.
How to convert JSON to a type ArrayList?
Many times we have a JOSN that we want to convert to a typed ArrayList or an ArrayList of custom class objects. This can be achieved using the fromJson
method of the Gson class along with defining a TypeToken of that custom type.
Here is such example JSON that we want to convert to an ArrayList of an employee type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[{ "id": "EMP001", "name": "Alex", "age": 22, "isManager": false, "reservedParking": null, "languages": ["English", "Spanish"] }, { "id": "EMP002", "name": "John", "age": 41, "isManager": true, "reservedParking": "PARK001", "languages": ["English", "German", "Italian"] }] |
We want to convert this JSON to an ArrayList object of type Employee. Here is an example code for that using the Gson library.
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
package com.javacodeexamples.gsonexamples; import java.util.ArrayList; import java.util.List; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class GsonConvertJSONToArrayListExample { public static void main(String[] args) { /* JSON data */ String jsonData = "[" + "{\"id\": \"EMP001\",\"name\": \"Alex\",\"age\": 22,\"isManager\": false,\"reservedParking\": null,\"languages\": [\"English\", \"Spanish\"]}, " + "{\"id\": \"EMP002\",\"name\": \"John\",\"age\": 41,\"isManager\": true,\"reservedParking\": \"PARK001\",\"languages\": [\"English\", \"German\", \"Italian\"]}" + "]"; /* * Create a TypeToken anonymous class of the same type, * in this case an ArrayList of Employee class */ TypeToken<ArrayList<Employee>> typeListEmployee = new TypeToken<ArrayList<Employee>>() {}; /* * Use the fromJson method of the Gson class and pass the JSON data * as well as the type we created earlier */ ArrayList<Employee> listEmployee = new Gson().fromJson(jsonData, typeListEmployee); for(Employee employee : listEmployee) { System.out.println(employee); } } } class Employee{ private String id; private String name; private int age; private boolean isManager; private String reservedParking; 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 boolean isManager() { return isManager; } public void setManager(boolean isManager) { this.isManager = isManager; } public String getReservedParking() { return reservedParking; } public void setReservedParking(String reservedParking) { this.reservedParking = reservedParking; } public List<String> getLanguages() { return languages; } public void setLanguages(List<String> languages) { this.languages = languages; } public String toString() { return "[" + this.getId() + ", " + this.getName() + ", " + this.getAge() + ", " + this.isManager + ", " + this.getReservedParking() + ", " + this.getLanguages() + "]"; } } |
Output
1 2 |
[EMP001, Alex, 22, false, null, [English, Spanish]] [EMP002, John, 41, true, PARK001, [English, German, Italian]] |
As we can see from the output, the example code converted JSON data to a typed ArrayList. Converting JSON to an array of custom objects is however simpler than that as we do not need to define the TypeToken in that case.
1 2 3 4 5 |
Employee[] arrayEmployee = new Gson().fromJson(jsonData, Employee[].class); for(Employee employee : arrayEmployee) { System.out.println(employee); } |
Output
1 2 |
[EMP001, Alex, 22, false, null, [English, Spanish]] [EMP002, John, 41, true, PARK001, [English, German, Italian]] |
Please let me know your views in the comments section below.