FastJson將json解析成含有泛型對象,內部泛型對象再次解析出錯的解決辦法(Android)

來源:互聯網
上載者:User

標籤:lan   lang   ble   技術分享   ges   roi   oid   style   time()   

折騰小半天的問題,這裡先感謝一下深圳的小夥子,遠程幫我搞,雖然也沒有搞出來==========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)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.