gpt4 book ai didi

java - Java中的多个JSON解析

转载 作者:行者123 更新时间:2023-11-29 03:44:01 44 4
gpt4 key购买 nike

我在一个文件 input.txt 中有多个 JSON:

{"Atlas":{"location":"lille","lat":28.4,"long":51.7,"country":"FR"}}
{"Atlas":{"location":"luxum","lat":24.1,"long":54.7,"country":"LU"}}
{"Atlas":{"location":"ghent","lat":28.1,"long":50.1,"country":"BE"}}

注意:它们不是用逗号 (",") 分隔的,它们不是数组。这些是有效的单个 JSON。

我相信应该可以得到输出。

我下面的代码既没有显示错误也没有给出输出?这里有什么问题?

这是我的代码:

 class Loc{
private String location;
private Long lat;
private Long long;
private String country;

//getter and setter methods
}
public class JsonReader {
public static void main(String[] args) throws ParseException {

try {
BufferedReader br= new BufferedReader(new FileReader("C:\\input.txt"));
String jsonTxt = IOUtils.toString( br );
JSONParser parser = new JSONParser();
String line=null;
while ((line = br.readLine()) != null)
{
Loc emp = (Loc) (new JSONParser().parse(jsonTxt));
System.out.printf("%s",emp.getLocation());
System.out.printf("\t%d",emp.getlat());
System.out.printf("\t%d",emp.getLong());
System.out.printf("\t%s",emp.getCountry()"\n");
}
}catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}}

最佳答案

您正在解析错误的内容。您应该解析 line 而不是 jsonTextline 是从 BufferedReader 读入的实际行。

关于java - Java中的多个JSON解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11823076/

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