折腾小半天的问题,这里先感谢一下深圳的小伙子,远程帮我搞,虽然也没有搞出来==========FUCK
声明:Android开发下发生此异常,Java开发下并不会有这个问题
异常重现
简单说一下抛出异常的代码:
(1)解析json代码如下:
1
2
|
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 如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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; } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
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
1
2
3
|
Gson gson =
new
Gson();
RecommendBean<RecommendListBean> obj = gson.fromJson(jsonString,
new
TypeToken<RecommendBean<RecommendListBean>>(){}.getType());
RecommendListBean recommendListBean = (RecommendListBean) (obj.getData());
|