如何使用Java中的Gson库遍历具有多个根元素的JSON文件?

凯玛特·罗奇(Kemat Rochi)

我有一个像这样的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"
}
{
    "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"
}

我想打印reviewText所有三个

我有这样的Java代码,

Java代码:

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

    }
}

但是我的代码仅输出第一项的输出。我如何打印reviewText所有三个

我的JSON文件格式正确吗?

小凤李

使用aJsonStreamParser处理文件中的多个顶级元素。

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用GSON解析不带根元素的JSON文件

在python中解析具有多个根元素的xml文件

使用具有多个根元素的XML文件

如何使用GSON库将json文件读入Java

使用gson库从Java属性文件中解析Json

如何使用 GSON 从同一个 JSON 文件中获取多个 Java 对象?

如何创建具有多个根元素的 XML

具有多个根元素的“ XML”文档

如何使用C#反序列化具有多个根的复杂JSON对象

如何在 Python 中循环使用具有多个子键的 JSON 文件?

如何通过gson解析具有多个对象的json数组?

使用Java脚本遍历具有多个子对象的JSON对象

使用 GSON 库解析后如何处理 json 元素?

如何使用R库遍历多个pdf文件

解析使用GSON Java中的JSON文件

使用gson在Java中写入json文件

在Java中使用gson具有漂亮打印格式的json文件I / O吗?

使用Java(具有多个元素值)将XML文件转换为CSV文件

如何在Java8中使用Lambda收集在多个列表中具有相同索引的元素

如何使用python遍历多个文件夹中的所有文件

如何使用 GSON 库在 Java 对象和 JSON 文件之间映射字段名称

如何在 Java 中在具有多个元素的对象列表中找到用于打印的特定元素?

循环遍历temp目录中的多个文件,并将文件插入到具有现有行/ ID的MS SQL数据库中

当XML具有特定的根元素名称时,如何将XML文件正确地读取到集合中?

Android:如何使用GSON库解析JSON文件?

使用 GSON 在 Java 中创建具有不同格式的嵌套 JSON 对象

如何使用工具:覆盖具有多个库的库

如何使用GSON在Android中获取具有特定值的JSON对象

如何从存储库索引中删除具有给定模式的多个文件夹?