且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Jackson在Java中解析JSON的子集

更新时间:2023-11-26 14:31:34

ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readTree("... your JSON ...");

使用 JsonNode 对象,然后可以调用 get(my)。get(deep)。get(structure)获取所需的节点。

Using the JsonNode object you can then call get("my").get("deep").get("structure") to get the node you want.

一旦你掌握了那个节点,只需简单调用 mapper.treeToValue(myDeepJsonNode,Telephone [] .class)就可以得到你的数组电话。您也可以使用 TypeReference 获取列表。

Once you got your hands on that node, a simple call to mapper.treeToValue(myDeepJsonNode, Telephone[].class) will get you your array ofTelephone. You can get a list using a TypeReference as well.

要深入 JsonNode 你也可以使用 findValue findPath 方法。

To get to your deep JsonNode you can also use the findValue and findPath methods.

Javadoc:
https://fasterxml.github.io/jackson-databind/javadoc/2.2.0/com/fasterxml/jackson/databind/JsonNode.html