gpt4 book ai didi

java - 如何在Java中解析具有分隔的json对象的文本文件?

转载 作者:行者123 更新时间:2023-12-02 12:48:13 24 4
gpt4 key购买 nike

我有一个文本文件,每 15-16 分钟就会更新一些 json 数据。这些 json 数据之间由 #### 行分隔。文件片段是:

[{"accountId":"abc","items":[{"serviceName":"XYZ","dataCenter":"TG","startTimeUtc":"2017-04-05T19:57:33.509+0000","endTimeUtc":"2017-04-05T19:57:33.509+0000","usage":[{"resourceName":"XYZ_EE_PAAS_GATEWAYS","quantity":7,"units":"number"}]}]},{"accountId":"XYZp1cm9mbe","items":[{"serviceName":"XYZ","dataCenter":"TG","startTimeUtc":"2017-04-05T19:57:33.509+0000","endTimeUtc":"2017-04-05T19:57:33.509+0000","usage":[{"resourceName":"XYZ_EE_PAAS_GATEWAYS","quantity":6,"units":"number"}]}]}]
######################
[{"accountId":"abc","items":[{"serviceName":"XYZ","dataCenter":"TG","startTimeUtc":"2017-04-05T19:59:33.523+0000","endTimeUtc":"2017-04-05T19:59:33.523+0000","usage":[{"resourceName":"XYZ_EE_PAAS_GATEWAYS","quantity":7,"units":"number"}]}]},{"accountId":"XYZp1cm9mbe","items":[{"serviceName":"XYZ","dataCenter":"TG","startTimeUtc":"2017-04-05T19:59:33.523+0000","endTimeUtc":"2017-04-05T19:59:33.523+0000","usage":[{"resourceName":"XYZ_EE_PAAS_GATEWAYS","quantity":6,"units":"number"}]}]}]
######################
[{"accountId":"abc","items":[{"serviceName":"XYZ","dataCenter":"TG","startTimeUtc":"2017-04-05T20:01:33.531+0000","endTimeUtc":"2017-04-05T20:01:33.531+0000","usage":[{"resourceName":"XYZ_EE_PAAS_GATEWAYS","quantity":7,"units":"number"}]}]},{"accountId":"XYZp1cm9mbe","items":[{"serviceName":"XYZ","dataCenter":"TG","startTimeUtc":"2017-04-05T20:01:33.531+0000","endTimeUtc":"2017-04-05T20:01:33.531+0000","usage":[{"resourceName":"XYZ_EE_PAAS_GATEWAYS","quantity":6,"units":"number"}]}]}]
######################

此文件每 15-16 分钟更新一次新条目。我想读取该文件并将最新条目存储在 json 对象中,不包括 #### 行。如何在java中做到这一点?我不想使用 15 分钟的间隔,因为它不是恒定的。

我的简单要求是在任何时候我都会读取文件并希望检索 ### 行上方的最后一个 json。

最佳答案

使用 Java 8,您可以这样做:

public JsonObject retrieveLastEntry(Path path) throws IOException {
String[] jsonLines = Files.lines(path)
.filter(line -> !line.equals("######################")
.toArray();
String lastJsonLine = jsonLines[jsonLines.length - 1];
return MyFavoriteJsonParser.parse(lastJsonLine);
}

MyFavoriteJsonParser 指的是您想要使用的任何 JSON 库(也许可以看看 this question )。这里可能有一些性能考虑因素。如果您的文件非常大(远大于几 MB),那么 .toArray() 调用可能不适合您。事实上,如果性能极其重要,您甚至可能需要考虑向后解析文件。但性能优化的黄金法则是首先采用一个简单的解决方案,看看它是否(以及在哪里)性能不够。

但是,如果您的 JSON 跨行,那么 Stream API 并不是最佳选择。在这种情况下,定期迭代可以解决问题:

public JsonObject retrieveLastEntry(File file) throws IOException {
String lastJson = "";
StringBuffer sb = new StringBuffer();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileReader(file), "UTF-8")))) {
String line;
while ((line = reader.readLine()) != null) {
if (line.equals("######################") {
lastJson = sb.toString(); sb.setLength(0);
} else {
sb.append(line).append('\n');
}
}
return MyFavoriteJsonParser.parse(lastJsonLine);
}

基本思想是聚合 ###... 之间的行,并在到达新分隔符时将它们放入变量中。您可能仍然需要考虑根本没有条目的情况并正确处理 IOException。

我认为这几乎是惯用的做法。

关于java - 如何在Java中解析具有分隔的json对象的文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44691760/

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