Skip to content

Gson – Convert JSON to a Typed ArrayList

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.

We want to convert this JSON to an ArrayList object of type Employee. Here is an example code for that using the Gson library.

Output

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.

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.