简体   繁体   English

如何使用 GSON 解析 JSON 文件

[英]how to parse JSON file with GSON

I have a very simple JSON with reviews for products, like:我有一个非常简单的 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."
}

I'd like to read it into my Java app using GSON.我想使用 GSON 将它读入我的 Java 应用程序。 I have built a class to hold results for each review:我已经建立了一个类来保存每次审查的结果:

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...

To read the JSON file, my code is:要读取 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

With this code, I can only retrieve the first review in the JSON, so my question is: how to iterate through all the reader and get the next reviews?使用此代码,我只能检索 JSON 中的第一个评论,所以我的问题是:如何遍历所有阅读器并获取下一个评论? I don't need to store the reviews in a List, just need to access the object once.我不需要将评论存储在列表中,只需要访问一次对象。 Any help more than welcome.任何帮助都超过欢迎。

You have to fetch the whole data in the list and then do the iteration as it is a file and will become inefficient otherwise.您必须获取列表中的全部数据,然后进行迭代,因为它是一个文件,否则效率会降低。

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

just parse as an array:只需解析为数组:

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

then if you need you can also create a list in this way:那么如果你需要,你也可以通过这种方式创建一个列表:

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

PS your json string should be look like this: PS你的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"
    },

    [...]
]

In case you need to parse it from a file , I find the best solution to use a HashMap<String, String> to use it inside your java code for better manipultion.如果您需要从文件中解析它,我找到了使用HashMap<String, String>在您的 java 代码中使用它以获得更好的操作的最佳解决方案。

Try out this code:试试这个代码:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM