有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何用GSON解析JSON文件

我有一个非常简单的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中的第一条评论,所以我的问题是:如何遍历所有读者并获得下一条评论?我不需要将评论存储在列表中,只需要访问该对象一次。欢迎任何帮助


共 (3) 个答案

  1. # 1 楼答案

    只需将其解析为数组:

    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"
        },
    
        [...]
    ]
    
  2. # 2 楼答案

    如果需要从文件中解析它,我发现最好的解决方案是使用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;
    }
    
  3. # 3 楼答案

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

    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