Toss a small half-day problem, here first thanked the lad of Shenzhen, remote help me to engage, although also did not get out ==========fuck
Disclaimer: This exception occurs under Android development and there is no problem with Java development
Abnormal recurrence
Simply say the code that throws the exception:
(1) Parse the JSON code as follows:
recommendbean<recommendlistbean> obj = Json.parseobject (jsonstring, New typereference<recommendbean< Recommendlistbean>> () {}); Recommendlistbean Recommendlistbean = (Recommendlistbean) (Obj.getdata ());
The error log is as follows:
Java.lang.ClassCastException:com.alibaba.fastjson.JSONObject cannot is cast to Com.test.RecommendListBean
(2) model as follows:
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; }}
Packagecom.test;Importjava.util.List;/*** Created by Lenovo on 2017/8/20.*/ Public classRecommendlistbean {Private intcount; PrivateList<recommendlistitembean>list; Public intGetCount () {returncount; } Public voidSetCount (intcount) { This. Count =count; } PublicList<recommendlistitembean>getList () {returnlist; } Public voidSetlist (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 = 21455356667 888L; 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.) {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; The 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 Serialversionui D = 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; } }}
Cause Analysis:
After debug found where generics are located, still jsonobject, and not resolved to the corresponding object, just to parse obj into the required entity object.
Even if a strong turn is used here, there will still be an error message: Cannot is cast to Com.test.RecommendListBean
Workaround: swap with Gson to parse, the specific method is as follows
Gson Gson = new Gson (); recommendbean<recommendlistbean> obj = Gson.fromjson (jsonstring, New typetoken<recommendbean< Recommendlistbean>> () {}.gettype ()); Recommendlistbean Recommendlistbean = (Recommendlistbean) (Obj.getdata ());
Fastjson parsing json into a solution that contains generic objects, with internal generic objects parsing errors again (Android)