的名单我需要解析存储在一个单一文件的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