使用嵌套List< Object>反序列化gson对象

使用嵌套List< Object>反序列化gson对象

问题描述:

public class ListResult<T> {

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

当我想反序列化给定的表单REST风格的JSON时,像这样:

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 ListResult,其中数据字段是StringMap的列表,而不是像ListResult中定义的T的List class

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

有什么想法?

Any idea ?

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

Gson 无法读懂您的想法,或者自行确定一个泛型类型;)

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