Skip to content

Gson – Parse JSON Array

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. Parsing JSON array to Java List

It is very easy to parse this JSON array to the List of Student class object using Gson.

Output

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.

Output

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.

Let’s parse this JSON member array as a Java list of student class objects with a list of subjects.

Output

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.