Java教程

Jackson中转换JsonNode(ArrayNode)到Java中的List对象

本文主要是介绍Jackson中转换JsonNode(ArrayNode)到Java中的List对象,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

https://stackoverflow.com/questions/48287292/how-do-you-convert-a-jackson-jsonnode-to-a-list-of-some-user-defined-object

{
  "objects":"that",
  "I":"dont care about",
  "objectsiwant":[{object1, object2,...}]
}

使用readerFor或readerForListOf

ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(json);
JsonNode internalNode = rootNode.path("objectsiwant");

// ----------------

List<MyPojo> myPojoList = mapper.readerFor(new TypeReference<List<MyPojo>>(){}).readValue(internalNode);

// 或者

List<MyPojo> myPojoList = mapper.readerForListOf(MyPojo.class).readValue(internalNode);
这篇关于Jackson中转换JsonNode(ArrayNode)到Java中的List对象的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!