Gson – Parse JSON array example shows how to parse a JSON array using the Gson library in Java. This example also shows how to parse JSON array to a Java array and JSON array to a Java list.
How to parse JSON array?
JSON array is a structure that stores multiple elements. For example, below given JSON array represents a list of students each having roll number and name properties.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[ { "rollNumber": 1, "name": "Alex" }, { "rollNumber": 2, "name": "John" }, { "rollNumber": 4, "name": "June" } ] |
1. Parsing JSON array to Java List
It is very easy to parse this JSON array to the List of Student class object using Gson.
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 |
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 GsonParseJSONArrayExample { public static void main(String[] args) { String jsonData = "[" + "{\"rollNumber\": 1,\"name\": \"Alex\"}," + "{\"rollNumber\": 2,\"name\": \"John\"}," + "{\"rollNumber\": 4,\"name\": \"June\"}" + "]"; //create Gson object Gson gson = new Gson(); //create type Type listType = new TypeToken<List<Student>>(){}.getType(); //read JSON array to a Java List List<Student> listStudents = gson.fromJson(jsonData, listType); System.out.println("There are " + listStudents.size() + " students"); //print JSON array elements for(Student student : listStudents) { System.out.println(student.getRollNumber() + " => " + student.getName()); } } } class Student{ private int rollNumber; private String name; public int getRollNumber() { return rollNumber; } public void setRollNumber(int rollNumber) { this.rollNumber = rollNumber; } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
Output
1 2 3 4 |
There are 3 students 1 => Alex 2 => John 4 => June |
As we can see from the output, each element of the JSON array is now parsed as a Java list element.
2. Parsing JSON array to Java Array
Parsing a JSON array to a Java array is very straightforward. We need to provide the type of array class when we parse the JSON array using the fromJson
method as given below.
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 |
package com.javacodeexamples.gsonexamples; import com.google.gson.Gson; public class GsonParseJSONArrayExample { public static void main(String[] args) { String jsonData = "[" + "{\"rollNumber\": 1,\"name\": \"Alex\"}," + "{\"rollNumber\": 2,\"name\": \"John\"}," + "{\"rollNumber\": 4,\"name\": \"June\"}" + "]"; //create Gson object Gson gson = new Gson(); //read JSON array to a Java array Student[] arrayStudents = gson.fromJson(jsonData, Student[].class); System.out.println("There are " + arrayStudents.length + " students"); //print array elements for(Student student : arrayStudents) { System.out.println(student.getRollNumber() + " => " + student.getName()); } } } class Student{ private int rollNumber; private String name; public int getRollNumber() { return rollNumber; } public void setRollNumber(int rollNumber) { this.rollNumber = rollNumber; } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
Output
1 2 3 4 |
There are 3 students 1 => Alex 2 => John 4 => June |
The above code parses the JSON array as a Java array and each element of the JSON array becomes an element of the Java array.
3. Parse JSON array as an object property
Consider the below given JSON where the subject is an array and is a property of the student.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[ { "rollNumber": 1, "name": "Alex", "subjects": ["Maths","Science"] }, { "rollNumber": 2, "name": "John", "subjects": ["Physics","Drawing"] }, { "rollNumber": 4, "name": "June", "subjects": ["Classical Dance","Music"] } ] |
Let’s parse this JSON member array as a Java list of student class objects with a list of subjects.
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 |
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 GsonParseJSONArrayExample { public static void main(String[] args) { String jsonData = "[" + "{\"rollNumber\": 1,\"name\": \"Alex\",\"subjects\": [\"Maths\",\"Science\"]}," + "{\"rollNumber\": 2,\"name\": \"John\",\"subjects\": [\"Physics\",\"Drawing\"]}," + "{\"rollNumber\": 4,\"name\": \"June\",\"subjects\": [\"Classical Dance\",\"Music\"]}" + "]"; //create Gson object Gson gson = new Gson(); //type of student class Type listType = new TypeToken<List<Student>>(){}.getType(); //read JSON to a Java list List<Student> listStudents = gson.fromJson(jsonData, listType); System.out.println("There are " + listStudents.size() + " students"); //print array elements for(Student student : listStudents) { System.out.println(student.getRollNumber() + " => " + student.getName() + ", subjects: " + student.getSubjects()); } } } class Student{ private int rollNumber; private String name; private List<String> subjects; public List<String> getSubjects() { return subjects; } public void setSubjects(List<String> subjects) { this.subjects = subjects; } public int getRollNumber() { return rollNumber; } public void setRollNumber(int rollNumber) { this.rollNumber = rollNumber; } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
Output
1 2 3 4 |
There are 3 students 1 => Alex, subjects: [Maths, Science] 2 => John, subjects: [Physics, Drawing] 4 => June, subjects: [Classical Dance, Music] |
Please let me know your views in the comments section below.