Gson convert JSON to list object

Gson - convert from Json to a typed ArrayList

You may use TypeToken to load the json string into a custom object.

logs = gson.fromJson[br, new TypeToken[]{}.getType[]];

Documentation:

Represents a generic type T.

Java doesn't yet provide a way to represent generic types, so this class does. Forces clients to create a subclass of this class which enables retrieval the type information even at runtime.

For example, to create a type literal for List, you can create an empty anonymous inner class:

TypeToken list = new TypeToken[] {};

This syntax cannot be used to create type literals that have wildcard parameters, such as Class or List

Chủ Đề