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

折騰小半天的問題,這裏先感謝一下深圳的小夥子,遠程幫我搞,雖然也沒有搞出來==========FUCKjava

聲明:Android開發下發生此異常,Java開發下並不會有這個問題json

異常重現ide

簡單說一下拋出異常的代碼:ui

(1)解析json代碼以下:this

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

錯誤日誌以下:spa

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

(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;
         }
     }
}

  

緣由分析:code

    

 

通過debug發現此處泛型所在的地方,仍然仍是JSONObject,而沒有解析成對應的對象,僅僅只是將obj解析成須要的實體對象了。對象

即便此處使用強轉,仍然會有報錯信息:cannot be cast to com.test.RecommendListBean

解決辦法:   換用GSON來解析,具體方法以下

1
2
3
Gson gson =  new  Gson();
RecommendBean<RecommendListBean> obj = gson.fromJson(jsonString,  new  TypeToken<RecommendBean<RecommendListBean>>(){}.getType());
RecommendListBean recommendListBean = (RecommendListBean) (obj.getData());
相關文章
相關標籤/搜索