Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize gson Object with nested List<Object>

public class ListResult<T> {

    private boolean ok;
    private String message;
    private java.util.List<T> data;
    private Paging paging;
}

When i want to deserialize JSON given form RESTful call like this :

Type fooType = new TypeToken<ListResult<T>>() {}.getType();
Gson gson = new Gson();
Object model = gson.fromJson(strResult, fooType);

A get A ListResult where data field is a list of StringMap instead a List of T like defined in ListResult class

Any idea ?

like image 400
user1379370 Avatar asked Apr 19 '26 08:04

user1379370


1 Answers

Type fooType = new TypeToken<ListResult<StringMap>>() {}.getType();
Gson gson = new Gson();
ListResult<StringMap> model = gson.fromJson(strResult, fooType);

Gson can't read your mind, or determine a Generic type all on its own ;)

https://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializing-Generic-Types

like image 200
Brian Roach Avatar answered Apr 29 '26 20:04

Brian Roach