JsonArray to Java List Gson Library
suggest changeHere is a JSON object whose list property is the array 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();
// Pass the JSON array text, such as the value of "list", not the surrounding object.
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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents