(转)https://segmentfault.com/q/1010000013320495
BaseBean里面的泛型如何转? 我的问题跟他一样,我的BaseBean是这样的
public class BaseBean<T> {
private String code;
private T data;
private String msg;
private String appversion;
private String cvs;
}
public class UserToken {
private String token;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
我拿到Json的字符串后,需要获取里面的token值,如果光这样写会报错
//错误写法
BaseBean<UserToken> userTokenBaseBean =gson.fromJson(jsonString,BaseBean.class);
//正确写法
BaseBean<UserToken> userTokenBaseBean =gson.fromJson(jsonString,new TypeToken<BaseBean<UserToken>>(){}.getType());
网友评论