Convert JSON Array to List of Long Type in Java
In this example tutorial, we will show you how to convert JSON array to List of Long type in Java.
To example requires Gson dependency.
import java.util.List;
import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class Example {
public static void main(String[] args) {
// Json array data
String jsonArrayExampleData = "[\"123\",\"456\",\"456\",\"232\"]";
// creating an object of Gson
Gson gson = new Gson();
// Converting to List of Long type
Type type = new TypeToken<List<Long>>() {
}.getType();
List<Long> list = gson.fromJson(jsonArrayExampleData, type);
System.out.println(list);
}
}
The output of the above example will look like this:
[123, 456, 456, 232]