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

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

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

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

    public int getCode() {
        return code;
    }

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

    public List getData() {
        return data;
    }

    public void setData(List 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 ;

    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){
        this.goods = goods;
    }
    public List 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 convertResponse(okhttp3.Response response) throws Throwable {
        ResponseBody responseBody = response.body();
        Gson gson = new Gson();
        JsonReader jsonReader = new JsonReader(responseBody.charStream());
        /**核心代码注意这代代码*/
        Type listType = new TypeToken>() {}.getType();
         /**使用 gson.fromJson(jsonReader, listType),
            而不是gson.fromJson(jsonReader, OrderAppResponseResult.class),
            因为传入OrderAppResponseResult会导致Gson泛型解析不出来*/
        OrderAppResponseResult
                appResponseResult = gson.fromJson(jsonReader, listType);
        return appResponseResult;
    }

你可能感兴趣的:(Gson解析json,android开发,JavaWeb)