gpt4 book ai didi

java - 如何从日志文件中读取json数据我将一些日志文件请给我java中的最佳解决方案

转载 作者:太空宇宙 更新时间:2023-11-04 13:17:20 25 4
gpt4 key购买 nike

我想读取日志文件。我只需要 json 数据。

Oct 30, 2015 1:48:30 PM SampleClient main
INFO: 2015-10-28T10:33:37+00:00 DEBUG (7): {"sku":"F00211","quantity":"1.0000","price":"355.0000","amount":"0.0000"}
2015-10-28T10:54:38+00:00 DEBUG (7): {"sku":"F00567","quantity":"1.0000","price":"550.0000","amount":"0.0000"}
2015-10-29T05:55:47+00:00 DEBUG (7): {"sku":"F00211","quantity":"2.0000","price":"355.0000","amount":"0.0000"}

FileReader fr = new FileReader("logsstore/log.log");
BufferedReader br = new BufferedReader(fr);

while (true)
{
String line = br.readLine();
if (line == null){
Thread.sleep(1*1000);}
else{
System.out.println(line);
}
}

最佳答案

看来你可以在每一行中搜索 { 并从那里开始获取子字符串。例如,修改代码中的 else block ,如下所示:

else {
int cbIndex = line.indexOf('{');
if (cbIndex != -1) {
String json = line.substring(cbIndex);
System.out.println(json);
}
}

这假设:

  1. 每一行都有一个 JSON 文档
  2. 此 JSON 末尾和该行末尾之前没有任何内容
  3. 每行 JSON 文档开始之前没有其他 {

关于java - 如何从日志文件中读取json数据我将一些日志文件请给我java中的最佳解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33432671/

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