小编典典

如何使用Jackson将JSON字符串解析为数组

json

我具有String以下值:

[
  {
    "key1": "value11",
    "key2": "value12"
  },
  {
    "key1": "value21",
    "key2": "value22"
  }
]

和以下类:

public class SomeClass {
    private String key1;
    private String key2;
    /* ... getters and setters omitted ...*/
}

我想将其解析为a List<SomeClass>或aSomeClass[]

使用Jackson的 方法最简单ObjectMapper


阅读 714

收藏
2020-07-27

共1个答案

小编典典

我终于明白了:

ObjectMapper objectMapper = new ObjectMapper();
TypeFactory typeFactory = objectMapper.getTypeFactory();
List<SomeClass> someClassList = objectMapper.readValue(jsonString, typeFactory.constructCollectionType(List.class, SomeClass.class));
2020-07-27