的名單我需要解析存儲在一個單一文件的JSON列表!解析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中只打印一個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"}}
您的幫助表示感謝!
一個有效的JSON文件必須包含一個根。所以要麼你把你的元素放在一個列表中,要麼將它們包裝在一個容器元素中。 – 2012-08-01 07:52:55
可能的重複線程 - http://stackoverflow.com/questions/11700482/convert-text-file-to-json-in-java http://stackoverflow.com/questions/11698447/json-parsing-through-multiple-對象http://stackoverflow.com/questions/11457856/json-read-file-parse-each-line-of-json-into-bean – adatapost 2012-08-01 07:56:10