Jackson multiple objects and huge json files

后端 未结 3 1624
长发绾君心
长发绾君心 2021-02-08 19:08

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

相关标签:
3条回答
  • 2021-02-08 19:58

    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.

    0 讨论(0)
  • 2021-02-08 20:01

    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();} }
    
    0 讨论(0)
  • 2021-02-08 20:01

    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.

    0 讨论(0)
提交回复
热议问题