Gson com.google.gson.internal.LinkedTreeMap cannot be cast to XXXXX bean

Gson使用过程中出现LikedTreeMap不能够转换成JavaBean

原因:由于传入时的类由于泛型引起的
看代码:

//服务器返回实体类
 public class AppResponseResult<T>{
    //状态码
    private int code;
    //返回数据
    private List<T> data;
    //返回消息
    private String msg;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}
//订单
public class OrderItem{
    private String orderId;

    private String createTime;

    private int isUrgent;

    private int status;

    private String linkMan;

    private String address;

    private String addressId;

    private String receiverPhone;

    private List<Goods> goods ;

    private int income;

    private int deliverStatus;

    private String deliverStatusStr;

    private String deliverName;

    private String deliverPhone;

    public void setOrderId(String orderId){
        this.orderId = orderId;
    }
    public String getOrderId(){
        return this.orderId;
    }
    public void setCreateTime(String createTime){
        this.createTime = createTime;
    }
    public String getCreateTime(){
        return this.createTime;
    }
    public void setIsUrgent(int isUrgent){
        this.isUrgent = isUrgent;
    }
    public int getIsUrgent(){
        return this.isUrgent;
    }
    public void setStatus(int status){
        this.status = status;
    }
    public int getStatus(){
        return this.status;
    }
    public void setLinkMan(String linkMan){
        this.linkMan = linkMan;
    }
    public String getLinkMan(){
        return this.linkMan;
    }
    public void setAddress(String address){
        this.address = address;
    }
    public String getAddress(){
        return this.address;
    }
    public void setAddressId(String addressId){
        this.addressId = addressId;
    }
    public String getAddressId(){
        return this.addressId;
    }
    public void setReceiverPhone(String receiverPhone){
        this.receiverPhone = receiverPhone;
    }
    public String getReceiverPhone(){
        return this.receiverPhone;
    }
    public void setGoods(List<Goods> goods){
        this.goods = goods;
    }
    public List<Goods> getGoods(){
        return this.goods;
    }
    public void setIncome(int income){
        this.income = income;
    }
    public int getIncome(){
        return this.income;
    }
    public void setDeliverStatus(int deliverStatus){
        this.deliverStatus = deliverStatus;
    }
    public int getDeliverStatus(){
        return this.deliverStatus;
    }
    public void setDeliverStatusStr(String deliverStatusStr){
        this.deliverStatusStr = deliverStatusStr;
    }
    public String getDeliverStatusStr(){
        return this.deliverStatusStr;
    }
    public void setDeliverName(String deliverName){
        this.deliverName = deliverName;
    }
    public String getDeliverName(){
        return this.deliverName;
    }
    public void setDeliverPhone(String deliverPhone){
        this.deliverPhone = deliverPhone;
    }
    public String getDeliverPhone(){
        return this.deliverPhone;
    }
}
//商品列表
public class Goods {
    private String goodsName;

    private int goodsNumber;

    private int price;

    public void setGoodsName(String goodsName){
        this.goodsName = goodsName;
    }
    public String getGoodsName(){
        return this.goodsName;
    }
    public void setGoodsNumber(int goodsNumber){
        this.goodsNumber = goodsNumber;
    }
    public int getGoodsNumber(){
        return this.goodsNumber;
    }
    public void setPrice(int price){
        this.price = price;
    }
    public int getPrice(){
        return this.price;
    }

}
 /**服务器返回数据进行解析*/
    public AppResponseResult<OrderItem> convertResponse(okhttp3.Response response) throws Throwable {
        ResponseBody responseBody = response.body();
        Gson gson = new Gson();
        JsonReader jsonReader = new JsonReader(responseBody.charStream());
        /**核心代码注意这代代码*/
        Type listType = new TypeToken<OrderAppResponseResult<OrderItem>>() {}.getType();
         /**使用 gson.fromJson(jsonReader, listType),
            而不是gson.fromJson(jsonReader, OrderAppResponseResult.class),
            因为传入OrderAppResponseResult会导致Gson泛型解析不出来*/
        OrderAppResponseResult<OrderItem>
                appResponseResult = gson.fromJson(jsonReader, listType);
        return appResponseResult;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值