如何使用Jackson将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

回答:

我终于明白了:

ObjectMapper objectMapper = new ObjectMapper();

TypeFactory typeFactory = objectMapper.getTypeFactory();

List<SomeClass> someClassList = objectMapper.readValue(jsonString, typeFactory.constructCollectionType(List.class, SomeClass.class));

以上是 如何使用Jackson将JSON字符串解析为数组 的全部内容, 来源链接: utcz.com/qa/422720.html

回到顶部