我有多個JSON在一個文件input.txt中:多JSON解析Java中
{"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();
}
}}
在它看起來像你在錯誤的情況下打開每個記錄乍看之下。 – 2012-08-06 16:46:24