単一のファイルに保存されているjsonのリストを解析する必要があります!
これまでに行ったことは、test.jsonファイルに次のものが含まれていることです。
{"location":"lille","lat":28.4,"long":51.7,"country":"FR"}
このファイルで私は以下のコードを持っています
public class JsonReader {
public static void main(String[] args) throws ParseException {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("c:\\test.json"));
JSONObject locationjson= (JSONObject) obj;
String location= (String) locationjson.get("location");
System.out.printf("%s",location);
long lat = (Long) locationjson.get("lat");
System.out.printf("\t%d",lat);
//similarly for other objects
これは機能するコードであり、ファイルtest.jsonに1つのjsonしか出力できません。
次に、ファイルにjsonのリストを出力する必要がある場合:test1.jsonは、次のようになります。各行は単一の有効なjsonであり、単一のファイルにjsonのリストがあります。必要なのは、各jsonを解析し、各行に出力することです。Beanクラスの使用は機能しますか?
{"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"}}
{"Atlas":{"location":"alborg","lat":23.4,"long":53.7,"country":"DN"}}
あなたの助けに感謝します!