Below code should work for your case.

List<Customer> customerList = CustomerDB.selectAll();

Gson gson = new Gson();
JsonElement element = gson.toJsonTree(customerList, new TypeToken<List<Customer>>() {}.getType());

if (! element.isJsonArray() ) {
// fail appropriately
    throw new SomeException();
}

JsonArray jsonArray = element.getAsJsonArray();

Heck, use List interface to collect values before converting it JSON Tree.

Answer from Ahsan Mahboob Shah on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-java-array-or-arraylist-to-jsonarray-using-gson-in-java
How to convert Java array or ArrayList to JsonArray using Gson in Java?
July 8, 2020 - import com.google.gson.*; import java.util.*; public class JavaArrayToJsonArrayTest { public static void main(String args[]) { String[][] strArray = {{"elem1-1", "elem1-2"}, {"elem2-1", "elem2-2"}}; ArrayList<ArrayList<String>> arrayList = new ArrayList<>(); for(int i = 0; i < strArray.length; i++) { ArrayList<String> nextElement = new ArrayList<>(); for(int j = 0; j < strArray[i].length; j++) { nextElement.add(strArray[i][j] + "-B"); } arrayList.add(nextElement); } JsonObject jsonObj = new JsonObject(); // array to JsonArray JsonArray jsonArray1 = new Gson().toJsonTree(strArray).getAsJsonArray(); // ArrayList to JsonArray JsonArray jsonArray2 = new Gson().toJsonTree(arrayList).getAsJsonArray(); jsonObj.add("jsonArray1", jsonArray1); jsonObj.add("jsonArray2", jsonArray2); System.out.println(jsonObj.toString()); } }
🌐
HowToDoInJava
howtodoinjava.com › home › gson › gson – parse json array to java array or list
Gson - Parse JSON Array to Java Array or List
April 4, 2023 - Here ArrayItem is the class type of data elements in the array. ArrayItem[] userArray = new Gson().fromJson(jsonSource, ArrayItem[].class); For converting such JSON array to a List, we need to use TypeToken class.
🌐
GitHub
gist.github.com › 21ec4a3a1fe9dc52e2cd195479428f98
JAVA array or ArrayList to JsonArray (Gson) · GitHub
JAVA array or ArrayList to JsonArray (Gson). GitHub Gist: instantly share code, notes, and snippets.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Converting JSON Array to Java List with Gson - Java Code Geeks
April 25, 2024 - We parse a JSON array into a JsonArray using Gson’s JsonParser. Then, we iterate over each element of the array, converting them into Java objects using Gson’s fromJson() method. Finally, we add each Java object to a List.
🌐
Crunchify
crunchify.com › json tutorials › in java how to convert arraylist to jsonobject?
In Java How to Convert ArrayList to JSONObject? • Crunchify
May 17, 2018 - <dependency> <groupId>com.goog... to latest version if you could. In our Java Program, we will create ArrayList first and then will convert it to JSONObject....
🌐
php.cn
m.php.cn › home › java › javatutorial › how to convert java array or arraylist to jsonarray in java using gson?
How to convert Java Array or ArrayList to JsonArray in Java using Gson?-javaTutorial-php.cn
September 8, 2023 - JsonArray can parse the text in a string to produce an object similar to a vector. We can use the toJsonTree().getAsJsonArray() method of the Gson class to convert an array or ArrayList into a JsonArray.
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java list › converting a java list to a json array
Converting a Java List to a Json Array | Baeldung
June 18, 2025 - In the above test method, we create a Gson instance (gson) and then use the toJson() method to convert the articles List to a JSON array represented as a string. Finally, we utilize the Assert class to verify the equality of the output jsonArray ...
Top answer
1 of 2
88

Here's a comprehensive example on how to use Gson with a list of objects. This should demonstrate exactly how to convert to/from Json, how to reference lists, etc.

Test.java:

import com.google.gson.Gson;
import java.util.List;
import java.util.ArrayList;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;


public class Test {

  public static void main (String[] args) {

    // Initialize a list of type DataObject
    List<DataObject> objList = new ArrayList<DataObject>();
    objList.add(new DataObject(0, "zero"));
    objList.add(new DataObject(1, "one"));
    objList.add(new DataObject(2, "two"));

    // Convert the object to a JSON string
    String json = new Gson().toJson(objList);
    System.out.println(json);

    // Now convert the JSON string back to your java object
    Type type = new TypeToken<List<DataObject>>(){}.getType();
    List<DataObject> inpList = new Gson().fromJson(json, type);
    for (int i=0;i<inpList.size();i++) {
      DataObject x = inpList.get(i);
      System.out.println(x);
    }

  }


  private static class DataObject {
    private int a;
    private String b;

    public DataObject(int a, String b) {
      this.a = a;
      this.b = b;
    }

    public String toString() {
      return "a = " +a+ ", b = " +b;
    }
  }

}

To compile it:

javac -cp "gson-2.1.jar:." Test.java

And finally to run it:

java -cp "gson-2.1.jar:." Test

Note that if you're using Windows, you'll have to switch : with ; in the previous two commands.

After you run it, you should see the following output:

[{"a":0,"b":"zero"},{"a":1,"b":"one"},{"a":2,"b":"two"}]
a = 0, b = zero
a = 1, b = one
a = 2, b = two

Keep in mind that this is only a command line program to demonstrate how it works, but the same principles apply within the Android environment (referencing jar libs, etc.)

2 of 2
0

My version of gson list deserialization using a helper class:

public List<E> getList(Class<E> type, JSONArray json) throws Exception {
    Gson gsonB = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

    return gsonB.fromJson(json.toString(), new JsonListHelper<E>(type));
}



public class JsonListHelper<T> implements ParameterizedType {

  private Class<?> wrapped;

  public JsonListHelper(Class<T> wrapped) {
    this.wrapped = wrapped;
  }

  public Type[] getActualTypeArguments() {
    return new Type[] {wrapped};
  }

  public Type getRawType() {
    return List.class;
  }

  public Type getOwnerType() {
    return null;
  }

}

Usage

List<Object> objects = getList(Object.class, myJsonArray);
Top answer
1 of 6
321

Definitely the easiest way to do that is using Gson's default parsing function fromJson().

There is an implementation of this function suitable for when you need to deserialize into any ParameterizedType (e.g., any List), which is fromJson(JsonElement json, Type typeOfT).

In your case, you just need to get the Type of a List<String> and then parse the JSON array into that Type, like this:

import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;

JsonElement yourJson = mapping.get("servers");
Type listType = new TypeToken<List<String>>() {}.getType();

List<String> yourList = new Gson().fromJson(yourJson, listType);

In your case yourJson is a JsonElement, but it could also be a String, any Reader or a JsonReader.

You may want to take a look at Gson API documentation.

2 of 6
19

Below code is using com.google.gson.JsonArray. I have printed the number of element in list as well as the elements in List

import java.util.ArrayList;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;


public class Test {

    static String str = "{ "+ 
            "\"client\":\"127.0.0.1\"," + 
            "\"servers\":[" + 
            "    \"8.8.8.8\"," + 
            "    \"8.8.4.4\"," + 
            "    \"156.154.70.1\"," + 
            "    \"156.154.71.1\" " + 
            "    ]" + 
            "}";

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {

            JsonParser jsonParser = new JsonParser();
            JsonObject jo = (JsonObject)jsonParser.parse(str);
            JsonArray jsonArr = jo.getAsJsonArray("servers");
            //jsonArr.
            Gson googleJson = new Gson();
            ArrayList jsonObjList = googleJson.fromJson(jsonArr, ArrayList.class);
            System.out.println("List size is : "+jsonObjList.size());
                    System.out.println("List Elements are  : "+jsonObjList.toString());


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

OUTPUT

List size is : 4

List Elements are  : [8.8.8.8, 8.8.4.4, 156.154.70.1, 156.154.71.1]
🌐
How to do in Java
howtodoinjava.com › home › convert json array to list in java: jackson, gson, org.json
Convert JSON Array to List in Java: Jackson, Gson, Org.json
2 weeks ago - Gson, developed by Google, is a small library with no dependencies that maps JSON to Java objects through reflection.
🌐
Stack Overflow
stackoverflow.com › questions › 61220004 › convert-arraylist-of-hashmaps-into-gson-jsonarray
java - Convert ArrayList of HashMaps into GSON JsonArray - Stack Overflow
April 15, 2020 - Type type = new TypeToken<ArrayList<HashMap<String, String>>>(){}.getType(); JsonElement element = new Gson().toJsonTree(x, type); JsonArray jsonArray = element.getAsJsonArray();
🌐
Rip Tutorial
riptutorial.com › jsonarray to java list (gson library)
Java Language Tutorial => JsonArray to Java List (Gson Library)
ArrayList<String> list = new Gson().fromJson(jsonList, listType); return list; } You should add the following maven dependency to your POM.xml file: <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> ...
🌐
Level Up Lunch
leveluplunch.com › java › examples › convert-json-array-to-arraylist-gson
Json array to ArrayList gson | Level Up Lunch
August 10, 2014 - We will initialize a java arraylist with two NavItems java objects by creating a Gson class from GSONBuilder class. The Gson class is the main class using Google Gson and can be built with various options.
🌐
Baeldung
baeldung.com › home › json › jackson › convert json array to java list
Convert JSON Array to Java List | Baeldung
August 13, 2025 - Gson is a widely-used JSON library for serializing and deserializing Java objects to and from JSON.