简体   繁体   English

在一个文件中解析多个JSON对象

[英]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. 我有多个JSON对象存储在一个用换行符分隔的文件中(但是一个对象可以跨越多行)-这是MongoDB shell的输出。

What is the easiest way to parse them (get them in an array or collection) using Gson and Java ? 使用GsonJava解析它们(将它们放入数组或集合中)的最简单方法是什么?

Another possibility is to use Jackson and its ObjectReader.readValues() methods: 另一种可能性是使用Jackson及其ObjectReader.readValues()方法:

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 在足够大(几千兆字节)的JSON数据文件上工作得很好

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

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