我試圖解析與GSON以下JSON響應:Deserialising與GSON JSON響應,使用Java
{
"total": 15,
"page": 2,
"pagesize": 10,
"rep_changes": [
{
"user_id": 2341,
"post_id": 2952530,
"post_type": "question",
"title": "unprotected access to member in property get",
"positive_rep": 5,
"negative_rep": 0,
"on_date": 1275419872
},
{
"user_id": 2341,
"post_id": 562948,
"post_type": "question",
"title": "Do you have any recommendations on Blend/XAML books/tutorials for designers?",
"positive_rep": 20,
"negative_rep": 0,
"on_date": 1270760339
}
....
}
這些是我已經定義(每個類包含相應getter和setter)的兩個類:
public class Reputation {
private int total;
private int page;
private int pagesize;
private List<RepChanges> rep_changes;
public class RepChanges {
private int user_id;
private int post_id;
private String post_type;
private String title;
private int positive_rep;
private int negative_rep;
private long on_date;
我一直無法將RepChanges類解析爲Reputation類中的rep_changes屬性。有什麼想法嗎?
注:我設法解析總頁數和頁面大小(這些是非複雜的屬性)。這是我做了什麼:
爲了解析總,頁面和頁面大小,我使用(運行良好):
Gson gson = new Gson();
Reputation rep = gson.fromJson(jsonText, Reputation.class);
//doing this works:
int page = rep.getPage();
System.out.println(page);
但是,這樣做的時候:
List<RepChanges> rep_changes = rep.getRep_changes();
for(RepChanges r : rep_changes){
System.out.println(r.toString());
}
我(技術上)沒有得到一個錯誤,但下面(這似乎是一個內存位置):
[email protected]
當您試圖反序列化它時,會出現什麼問題? – ColinD 2012-03-11 17:33:45
@colinD,我編輯了這個問題,我進一步解釋了一下......希望能給出更好的見解;) – savagius 2012-03-11 17:42:44