如何在春季使用杰克逊将jsonarray转换为字符串数组
The necessary bits to build these optional modules were not found:
_tkinter
这应该使用杰克逊转换为对象的array []
答案 0 :(得分:1)
除了事实,将数字数组转换为字符串数组似乎没有什么用,杰克逊会毫无问题地读到这一点:
String json = "{\"number\":[5,3,6,7,9,8,10,11,12,13,14,15,19,26]}";
ObjectMapper mapper = new ObjectMapper();
MyObject object = mapper.readValue(json, MyObject.class);
MyObject
如下所示:
private static class MyObject {
private String[] number;
public String[] getNumber() {
return number;
}
public void setNumber(String[] number) {
this.number = number;
}
}
要仅获取数组,可以使用mapper.readTree()
来获取JsonNode。之后,您可以提取数组并使用java流API对其进行解析:
String[] array = StreamSupport.stream(mapper.readTree(json).get("number").spliterator(), false)
.map(JsonNode::asText)
.toArray(String[]::new);