原先代码:
public class GsonUtil<T> {
private static Gson gson=new Gson();
//Gson将接收的String转换为User对象
public User fromGson(String str){
return gson.fromJson(str, User.class);
}
}
服务端无法转成User
解决方法:使用
public class GsonUtil<T> {
private static Gson gson=new Gson();
//Gson将接收的String转换为T对象
public T fromGson(String str){
Type type=new TypeToken<T>(){}.getType();
return gson.fromJson(str, type);
}
}
详情看外文链接:http://stackoverflow.com/questions/19174340/how-to-iterate-object-array-gson-deserialize