How to deserialize JSON file starting with an array in Jackson?

后端 未结 3 1815
我寻月下人不归
我寻月下人不归 2020-12-18 18:43

I have a Json file that looks like this:

[
    { \"field\":\"val\" },
....
]

I have Java object representing single object and collection o

3条回答
  •  执念已碎
    2020-12-18 19:34

    Passing an array or List type works, as @dmon answered.

    For sake of completeness, there is also an incremental approach if you wanted to read contents one-by-one:

    Iterator it = mapper.reader(Objects.class).readValues(in);
    while (it.hasNext()) {
      Objects next = it.next();
      // ... process it
    }
    

    This is useful if you have huge lists or sequences of objects; either with enclosing JSON Array, or just root-level values separated by spaces or linefeeds.

提交回复
热议问题