如何使用Java中的Gson库迭代一个包含多个根元素的JSON文件?

时间:2021-07-15 15:26:59

I have a JSON file like this,

我有一个像这样的JSON文件,

JSON File

{
    "reviewerID": "A7S2B0I67WNWB", 
    "asin": "0594481813", 
    "reviewerName": "AllyMG", 
    "helpful": [2, 2], 
    "reviewText": "This item is just as was described in the original description, works without any issues to be seen. Good product", 
    "overall": 4.0, 
    "summary": "As expected", 
    "unixReviewTime": 1397606400, 
    "reviewTime": "04 16, 2014"
}
{
    "reviewerID": "A3HICVLF4PFFMN", 
    "asin": "0594481813", 
    "reviewerName": "Amazon Customer", 
    "helpful": [0, 0], 
    "reviewText": "bought for a spare for my 9" Nook HD and it fit perfectly.  Very satisfied with the price much less than on the BN site", 
    "overall": 5.0, 
    "summary": "great fit", 
    "unixReviewTime": 1399248000, 
    "reviewTime": "05 5, 2014"
}
{
    "reviewerID": "ANSKSPEEAKY7S", 
    "asin": "0594481813", 
    "reviewerName": "Gena", 
    "helpful": [1, 1], 
    "reviewText": "My son crewed my HD charger cord so I needed another one, this is exactly like the one my son destroyed.", 
    "overall": 5.0, 
    "summary": "Works Great", 
    "unixReviewTime": 1372032000, 
    "reviewTime": "06 24, 2013"
}

And I want to print the values of reviewText in all three of them.

我想在所有这三个中打印reviewText的值。

I have a Java code like this,

我有这样的Java代码,

Java Code:

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class ReviewText {

    public static void main(String[] args) throws FileNotFoundException {

        JsonParser parser = new JsonParser();
        JsonReader jsonReader = new JsonReader(new FileReader("sample.json"));

        JsonElement jsonTree = parser.parse(jsonReader);

        JsonObject jsonObject = jsonTree.getAsJsonObject();

        JsonElement f1 = jsonObject.get("reviewText");

        System.out.println(f1);

    }
}

But my code prints the output only for the first item. How do I print the value of reviewTextfor all three of them?

但我的代码只打印第一项的输出。如何为所有这三个打印reviewText的值?

Is my JSON file in the correct format?

我的JSON文件格式是否正确?

2 个解决方案

#1


6  

Use a JsonStreamParser to handle multiple top-level elements in the file.

使用JsonStreamParser处理文件中的多个*元素。

JsonStreamParser parser = new JsonStreamParser(new FileReader("sample.json"));

while (parser.hasNext()) {
    JsonElement object = parser.next();
    System.out.println(object.getAsJsonObject().get("reviewText"));
}

#2


1  

If your JSON file were structured like this, [{}, {}, {}, ...], you could read it as a JSON array and iterate through it.

如果您的JSON文件的结构如下,[{},{},{},...],您可以将其作为JSON数组读取并迭代它。

[
    {
        "reviewerID": "A7S2B0I67WNWB", 
        "asin": "0594481813", 
        "reviewerName": "AllyMG", 
        "helpful": [2, 2], 
        "reviewText": "This item is just as was described in the original description, works without any issues to be seen. Good product", 
        "overall": 4.0, 
        "summary": "As expected", 
        "unixReviewTime": 1397606400, 
        "reviewTime": "04 16, 2014"
    },
    {
        "reviewerID": "A3HICVLF4PFFMN", 
        "asin": "0594481813", 
        "reviewerName": "Amazon Customer", 
        "helpful": [0, 0], 
        "reviewText": "bought for a spare for my 9" Nook HD and it fit perfectly.  Very satisfied with the price much less than on the BN site", 
        "overall": 5.0, 
        "summary": "great fit", 
        "unixReviewTime": 1399248000, 
        "reviewTime": "05 5, 2014"
    }
]

#1


6  

Use a JsonStreamParser to handle multiple top-level elements in the file.

使用JsonStreamParser处理文件中的多个*元素。

JsonStreamParser parser = new JsonStreamParser(new FileReader("sample.json"));

while (parser.hasNext()) {
    JsonElement object = parser.next();
    System.out.println(object.getAsJsonObject().get("reviewText"));
}

#2


1  

If your JSON file were structured like this, [{}, {}, {}, ...], you could read it as a JSON array and iterate through it.

如果您的JSON文件的结构如下,[{},{},{},...],您可以将其作为JSON数组读取并迭代它。

[
    {
        "reviewerID": "A7S2B0I67WNWB", 
        "asin": "0594481813", 
        "reviewerName": "AllyMG", 
        "helpful": [2, 2], 
        "reviewText": "This item is just as was described in the original description, works without any issues to be seen. Good product", 
        "overall": 4.0, 
        "summary": "As expected", 
        "unixReviewTime": 1397606400, 
        "reviewTime": "04 16, 2014"
    },
    {
        "reviewerID": "A3HICVLF4PFFMN", 
        "asin": "0594481813", 
        "reviewerName": "Amazon Customer", 
        "helpful": [0, 0], 
        "reviewText": "bought for a spare for my 9" Nook HD and it fit perfectly.  Very satisfied with the price much less than on the BN site", 
        "overall": 5.0, 
        "summary": "great fit", 
        "unixReviewTime": 1399248000, 
        "reviewTime": "05 5, 2014"
    }
]