简体   繁体   中英

Using jackson to traverse json tree

I'm developing a new system that talks to a third party via JSON.

One of the calls returns a huge JSON structure to represent products and rules.

I've used Jackson to convert this JSON into a tree quite easily. Now the issue is I want to be able to find nodes by 'querying' without manually traversing the whole tree.

So somewhere deep in the tree is an object which has a field called business_id. I want to return all the nodes that have this field.

Is that possible?

You can use Jackson's JsonNode class documented here: http://fasterxml.github.io/jackson-databind/javadoc/2.5/com/fasterxml/jackson/databind/JsonNode.html

Parse your data into a JsonNode (eg by ObjectMapper.readValue), then you can traverse programmatically that JSON structure as a tree.

Look at methods like: as{datatype}, find[Value|Values], is[Array|Object|{datatype}], path etc.

You could try Json Path, it lets you pick up a json node using it's xpath:

http://code.google.com/p/json-path/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM