I get the feeling that the answer might be a duplicate of this: Jackson - Json to POJO With Multiple Entries but I think that potentially the question is different enough. Also
Assuming you have an array wrapping your objects, create a JsonParser and then call readValuesAs
with the appropriate type. It gives you back an Iterator
with all your objects that reads through the file as you consume the objects.
Use this code sample to see the basic idea.
final InputStream in = new FileInputStream("json.json");
try {
for (Iterator it = new ObjectMapper().readValues(
new JsonFactory().createJsonParser(in), Map.class); it.hasNext();)
System.out.println(it.next());
}
finally { in.close();} }
You don't have to choose between Streaming (JsonParser
) and ObjectMapper
, do both!
Traverse a bit with parser, but then call JsonParser.readValueAs(MyType.class)
to bind individual JSON Object.
Or, call ObjectMapper
's readValue()
method passing JsonParser at appropriate points. Or use ObjectMapper.reader(Type.class).readValues()
and iterate that way.