Java Language JsonArray to Java List (Gson Library)


Example

Here is a simple JsonArray which you would like to convert to a Java ArrayList:

{
    "list": [
                "Test_String_1",
                "Test_String_2"
            ] 
}

Now pass the JsonArray 'list' to the following method which returns a corresponding Java ArrayList:

public ArrayList<String> getListString(String jsonList){
    Type listType = new TypeToken<List<String>>() {}.getType();
    //make sure the name 'list' matches the name of 'JsonArray' in your 'Json'.
    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>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.7</version>
</dependency>

Or you should have the jar com.google.code.gson:gson:jar:<version> in your classpath.