简体   繁体   English

Java中的多个JSON解析

[英]Multiple JSON parsing in Java

I have multiple JSON in a single file input.txt: 我在单个文件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"}}

NOTE: they are not separated by comma (",") and they are not array. 注意:它们不用逗号(“,”)分隔,它们不是数组。 These are valid single JSON. 这些是有效的单JSON。

I believe it should be possible to get an output. 我相信它应该可以获得输出。

My code below neither shows an error nor gives an output? 我下面的代码既没有显示错误也没有输出? What is wrong here? 这有什么不对?

This is my code: 这是我的代码:

 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();
        }

}}

You're parsing the wrong thing. 你正在解析错误的东西。 You should be parsing line not jsonText . 你应该解析line而不是jsonText line is the actual line read in from the BufferedReader. line是从BufferedReader读入的实际行。

You're passing the result of IOUtils.toString(BufferedReader) to the JSONParser . 您将IOUtils.toString(BufferedReader)的结果传递给JSONParser As you've noted, the entire file content isn't valid JSON. 如您所知,整个文件内容无效JSON。 Assuming that each line is valid JSON, pass the data line-by-line to the parser. 假设每一行都是有效的JSON,将数据逐行传递给解析器。 Your code is already set up to do this. 您的代码已经设置为执行此操作。

In your loop, pass line to the parser, and get rid of the IOUtils stuff. 在你的循环中,将line传递给解析器,并摆脱IOUtils东西。

I assume that JSONParser is org.json.simple.parser.JSONParser . 我假设JSONParserorg.json.simple.parser.JSONParser If so, it won't build your Loc objects for you without more work on your part: it only parses the JSON and returns a tree of JSONObjects , JSONArray , etc. See here and the bit about JSONAware here . 如果是这样,它不会建立自己的Loc无需您再为你的对象:只解析JSON和返回的树JSONObjectsJSONArray等见这里和对位JSONAware 这里

Or look into Jackson or GSON for parsing your JSON into java objects. 或者查看JacksonGSON将JSON解析为java对象。

Also, your Loc member can't be named long : that's a java keyword, and will prevent your code from compiling. 此外,您的Loc成员不能命名为long :这是一个java关键字,将阻止您的代码编译。

看起来你需要解析行,而不是jsonTxt

Loc emp = (Loc) (new JSONParser().parse(jsonTxt));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM