2015-04-30 64 views
24

我有一個非常簡單的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檢索第一次審查,所以我的問題是:如何迭代所有讀者並獲得下一個評論?我不需要將評論存儲在列表中,只需訪問一次對象。任何幫助比歡迎。

+0

將你收到的字符串解析成一個新的JSONArray()。對於數組中的每個對象,執行gson.fromJson(object,Review.class),然後將它們全部添加到一個空列表中。如在,順序解析它們。 – Schaka

回答

42

您必須獲取列表中的所有數據,然後執行迭代,因爲它是一個文件,否則將變得效率低下。

private static final Type REVIEW_TYPE = new TypeToken<List<Review>>() { 
}.getType(); 
Gson gson = new Gson(); 
JsonReader reader = new JsonReader(new FileReader(filename)); 
List<Review> data = gson.fromJson(reader, REVIEW_TYPE); // contains the whole reviews list 
data.toScreen(); // prints to screen some values 
+1

我的代碼有這樣的錯誤:java.lang.IllegalStateException:期望的BEGIN_ARRAY,但是BEGIN_OBJECT在第1行第2列路徑$ – user299791

+1

檢查你的json文件格式是否正確或者不在[jsonlint](http:// jsonlint。 com) –

+1

@Archit Maheshwari - 輸入格式正確且有效。它包含兩個類型爲'object'的JSON數據結構。這是你的例子,不工作,至少不與gson 2.8.0。儘管如此,我相信你的例子提供了一個有趣的方法來解決這個問題。我建議將它添加到Gson的願望清單中。 – whaefelinger

17

只是解析爲一個數組:

Review[] reviews = new Gson().fromJson(jsonString, Review[].class); 

那麼,如果你需要,你也可以通過這種方式創建一個列表:

List<Review> asList = Arrays.asList(reviews); 

附:你的json字符串應該是這樣的:

[ 
    { 
     "reviewerID": "A2SUAM1J3GNN3B1", 
     "asin": "0000013714", 
     "reviewerName": "J. McDonald", 
     "helpful": [2, 3], 
     "reviewText": "I bought this for my husband who plays the piano.", 
     "overall": 5.0, 
     "summary": "Heavenly Highway Hymns", 
     "unixReviewTime": 1252800000, 
     "reviewTime": "09 13, 2009" 
    }, 
    { 
     "reviewerID": "A2SUAM1J3GNN3B2", 
     "asin": "0000013714", 
     "reviewerName": "J. McDonald", 
     "helpful": [2, 3], 
     "reviewText": "I bought this for my husband who plays the piano.", 
     "overall": 5.0, 
     "summary": "Heavenly Highway Hymns", 
     "unixReviewTime": 1252800000, 
     "reviewTime": "09 13, 2009" 
    }, 

    [...] 
] 
+0

我的代碼出現錯誤:com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:第1行第1列路徑的期望值$ – user299791

+1

向我們顯示您的REAL json字符串的第一行 – dit

+0

對不起,我的文件是3GB,我試圖加載VIM,但無法加載它 – user299791