FastJson将json解析成含有泛型对象,内部泛型对象再次解析出错的解决办法(Android)

Posted 纳尼纳闷

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FastJson将json解析成含有泛型对象,内部泛型对象再次解析出错的解决办法(Android)相关的知识,希望对你有一定的参考价值。

折腾小半天的问题,这里先感谢一下深圳的小伙子,远程帮我搞,虽然也没有搞出来==========FUCK

声明:android开发下发生此异常,Java开发下并不会有这个问题

异常重现

简单说一下抛出异常的代码:

(1)解析json代码如下:

RecommendBean<RecommendListBean> obj = JSON.parseObject(jsonString, new TypeReference<RecommendBean<RecommendListBean>>(){});
RecommendListBean recommendListBean = (RecommendListBean) (obj.getData());

错误日志如下:

java.lang.ClassCastException: com.alibaba.fastjson.JSONObject cannot be cast to com.test.RecommendListBean

(2)model 如下:

package com.test;

/**
 * Created by Travis on 2017/8/20.
 */

public class RecommendBean<T> {

    private int status;
    private String message;
    private T data;

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

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

  

package com.test;

import java.util.List;

/**
 * Created by lenovo on 2017/8/20.
 */

public class RecommendListBean {

    private int count;
    private List<RecommendListItemBean> list;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public List<RecommendListItemBean> getList() {
        return list;
    }

    public void setList(List<RecommendListItemBean> list) {
        this.list = list;
    }





}

 

package com.test;

import java.io.Serializable;

/**
 * Created by Travis on 2017/8/20.
 */

public class RecommendListItemBean implements Serializable {
    private static final long serialVersionUID = 21455356667888L;

    private int uid;
    private String nickname;
    private int sex;
    private int age;
    private String avatar;
    private String theme_pic;
    private String job;
    private String signature;
    private String sound;
    private int sound_length;
    private int fee;

    public RecommendBean getRecommend() {
        return recommend;
    }

    public void setRecommend(RecommendBean recommend) {
        this.recommend = recommend;
    }

    private RecommendBean recommend;

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getTheme_pic() {
        return theme_pic;
    }

    public void setTheme_pic(String theme_pic) {
        this.theme_pic = theme_pic;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
    }


    public static class RecommendBean<T> implements Serializable {
        private static final long serialVersionUID = 21455356667889L;

        private int id;
        private int user_id;
        private int type;
        private Object content;
        private int zan;
        private int status;
        private String create_time;
        private String update_time;
        private int zanTotal;
        private int commentTotal;
        private T picture;
        private T video;

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getUser_id() {
            return user_id;
        }

        public void setUser_id(int user_id) {
            this.user_id = user_id;
        }

        public Object getContent() {
            return content;
        }

        public void setContent(Object content) {
            this.content = content;
        }

        public int getZan() {
            return zan;
        }

        public void setZan(int zan) {
            this.zan = zan;
        }

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public String getCreate_time() {
            return create_time;
        }

        public void setCreate_time(String create_time) {
            this.create_time = create_time;
        }

        public String getUpdate_time() {
            return update_time;
        }

        public void setUpdate_time(String update_time) {
            this.update_time = update_time;
        }

        public int getZanTotal() {
            return zanTotal;
        }

        public void setZanTotal(int zanTotal) {
            this.zanTotal = zanTotal;
        }

        public int getCommentTotal() {
            return commentTotal;
        }

        public void setCommentTotal(int commentTotal) {
            this.commentTotal = commentTotal;
        }

        public T getPicture() {
            return picture;
        }

        public void setPicture(T picture) {
            this.picture = picture;
        }

        public T getVideo() {
            return video;
        }

        public void setVideo(T video) {
            this.video = video;
        }
    }
}

  

原因分析:

    

 

经过debug发现此处泛型所在的地方,仍然还是JSONObject,而没有解析成对应的对象,仅仅只是将obj解析成需要的实体对象了。

即使此处使用强转,仍然会有报错信息:cannot be cast to com.test.RecommendListBean

解决办法:   换用GSON来解析,具体方法如下

  Gson gson = new Gson();
  RecommendBean<RecommendListBean> obj = gson.fromJson(jsonString, new TypeToken<RecommendBean<RecommendListBean>>(){}.getType());
  RecommendListBean recommendListBean = (RecommendListBean) (obj.getData());

  

 

以上是关于FastJson将json解析成含有泛型对象,内部泛型对象再次解析出错的解决办法(Android)的主要内容,如果未能解决你的问题,请参考以下文章

fastjson怎么解析像“

JSON 转含有泛型属性的对象

fastjson进行json的解析和序列化

JSON_常用类JSON与对象的互相转换TypeReference泛型遇到的坑

JSON_常用类JSON与对象的互相转换TypeReference泛型遇到的坑

如何解决fastjson把对象转化成json避免$ref