1つのファイル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();
}
}}