编程语言
首页 > 编程语言> > 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);

标签:mapper,JsonNode,Java,readValue,List,ObjectMapper,internalNode
来源: https://www.cnblogs.com/im_bennie/p/15637960.html