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



我有一个文本文件,每 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库(也许看看这个问题)。这里可能很少有性能注意事项。如果您的文件非常大(远远超过几 MB),那么.toArray()调用可能不适合您。事实上,如果性能非常关键,您甚至可能需要考虑向后解析文件。但是性能优化的黄金法则是首先使用一个简单的解决方案,看看它是否(以及在何处)性能不足。

但是,如果您的 JSON 跨行,则流 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

我认为这几乎是惯用的方式。

最新更新