如何用GSON解析JSON文件

bfnvny8b  于 2022-11-06  发布在  其他
关注(0)|答案(3)|浏览(268)

我有一个非常简单的JSON,其中包含产品评论,例如:

{
  "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中检索第一篇评论,所以我的问题是:如何遍历所有的reader并获得下一个评论?我不需要在List中存储评论,只需要访问对象一次。任何帮助都比欢迎更多。

5jdjgkvh

5jdjgkvh1#

您必须获取列表中的全部数据,然后进行迭代,因为它是一个文件,否则将变得低效。

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
lawou6xi

lawou6xi2#

只解析为数组:

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

那么如果需要,您还可以通过以下方式创建列表:

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

P.S.你的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"
    },

    [...]
]
h79rfbju

h79rfbju3#

如果你需要从一个文件中解析它,我发现最好的解决方案是使用一个HashMap<String, String>在你的java代码中使用它,以获得更好的操作。
请尝试以下代码:

public HashMap<String, String> myMethodName() throws FileNotFoundException
{
    String path = "absolute path to your file";
    BufferedReader bufferedReader = new BufferedReader(new FileReader(path));

    Gson gson = new Gson();
    HashMap<String, String> json = gson.fromJson(bufferedReader, HashMap.class);
    return json;
}

相关问题