gpt4 book ai didi

java - 使用其他 JSON 对象过滤 JSON 中的键和值

转载 作者:行者123 更新时间:2023-11-30 06:51:12 25 4
gpt4 key购买 nike

我目前遇到以下问题:

我希望循环访问一组 JSON 文件。我想过滤掉与过滤器匹配的某些 JSON 文件。此过滤器是另一个 JSON 对象。

MongoDB 能够做到这一点;您提供一个 JSON 对象作为参数,它将列出包含给定 JSON 元素的文档。

我需要这个的平面文件版本,但我无法成功。我使用 GSON 作为我的 JSON 库。

最佳答案

使用文件路径数组,每个文件路径包含一个 JSON 字符串和一个表示过滤规则的 JsonObject。返回符合过滤规则的文件路径列表。

public List<String> filter(String[] filePaths, JsonObject rules) throws FileNotFoundException {
final List<String> filtered = new ArrayList<String>();
final Set<Map.Entry<String, JsonElement>> rulesEntries = rules.entrySet();
for (String path : filePaths) {
final Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(path))));
final JsonObject file = jsonParser.parse(reader).getAsJsonObject();
final Set<Map.Entry<String, JsonElement>> fileEntries = file.entrySet();
if (fileEntries.containsAll(rulesEntries)) filtered.add(path);
}
return filtered;
}

关于java - 使用其他 JSON 对象过滤 JSON 中的键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42739650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com