Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap c...

(转)https://segmentfault.com/q/1010000013320495
BaseBean里面的泛型如何转? 我的问题跟他一样,我的BaseBean是这样的

public class BaseBean {
    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 userTokenBaseBean =gson.fromJson(jsonString,BaseBean.class);
//正确写法
 BaseBean userTokenBaseBean =gson.fromJson(jsonString,new TypeToken>(){}.getType());

你可能感兴趣的:(Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap c...)