简体   繁体   中英

Parse multiple JSON objects in one file

I have multiple JSON objects stored in one file separated by new line character (but one object can span over multiple lines) - it's an output from MongoDB shell.

What is the easiest way to parse them (get them in an array or collection) using Gson and Java ?

Another possibility is to use Jackson and its ObjectReader.readValues() methods:

public <T> Iterator<T> readStream(final InputStream _in) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    // configure object mappings
    ...
    // and then
    return mapper.reader(MapObject.class).readValues(_in);
}

works pretty good on big enough (few gigabytes) JSON datafiles

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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