我在单个文件 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();
}
}}