我有一個非常簡單的JSON與產品評論,如:如何解析JSON文件,GSON
{
"reviewerID": "A2XVJBSRI3SWDI",
"asin": "0000031887",
"reviewerName": "abigail",
"helpful": [0, 0],
"unixReviewTime": 1383523200,
"reviewText": "Perfect red tutu for the price. ",
"overall": 5.0,
"reviewTime": "11 4, 2013", "summary": "Nice tutu"
}
{
"reviewerID": "A2G0LNLN79Q6HR",
"asin": "0000031887",
"reviewerName": "aj_18 \"Aj_18\"",
"helpful": [1, 1],
"unixReviewTime": 1337990400,
"reviewText": "This was a really cute",
"overall": 4.0,
"reviewTime": "05 26, 2012",
"summary": "Really Cute but rather short."
}
我想讀取到使用GSON我的Java應用程序。我建立了一個類來保存每個審覈結果:
public class Review {
private String reviewerID;
private String asin;
private String reviewerName;
private ArrayList<Integer> helpful;
private String reviewText;
private Double overall;
private String summary;
private Long unixReviewTime;
private String reviewTime;
public Review() {
this.helpful = Lists.newArrayList();
}
// some getters and setters...
要讀取JSON文件,我的代碼是:
Gson gson = new Gson();
JsonReader reader = new JsonReader(new FileReader(filename));
Review data = gson.fromJson(reader, Review.class);
data.toScreen(); // prints to screen some values
有了這個代碼,我只能在JSON檢索第一次審查,所以我的問題是:如何迭代所有讀者並獲得下一個評論?我不需要將評論存儲在列表中,只需訪問一次對象。任何幫助比歡迎。
將你收到的字符串解析成一個新的JSONArray()。對於數組中的每個對象,執行gson.fromJson(object,Review.class),然後將它們全部添加到一個空列表中。如在,順序解析它們。 – Schaka