日期:2014-05-20 浏览次数:21498 次
protected <T extends Model> List<T> PostData(String serviceName, SearchInfo info)
throws ApiException {
//其中builder是一个Json字符串
Type t = new TypeToken<List<T>>() {
}.getType();
// Create a new Model from the JSON data.
List<T> list = new Gson().fromJson(
builder, t);
}
调用方法是这样调用的this.<AccessStatistics>PostData("test",info)
//其中AccessStatistics是Model的一个子类,info是SearchInfo的实例化对象
package test;
import java.util.ArrayList;
import java.util.List;
public class Node extends IModel{
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
System.out.println(new Node().<Node>test(Node.class));
}
public <T extends IModel> List<T> test(Class<T> clazz) throws InstantiationException, IllegalAccessException{
List<T> list= new ArrayList<T>();
T obj = clazz.newInstance();
list.add(obj);
System.out.println(list.get(0).getClass());
return list;
}
}
class IModel{
}
------解决方案--------------------
List<T> list = new Gson().<AccessStatistics>fromJson(builder.toString(), t);
------解决方案--------------------
错了,第2个参数就是类型了,
你可以换成这个试试
fromJson(JsonElement json, Class<T> classOfT)