What is the role of the JsonNode class in Jackson?
A) To represent a node in a JSON tree structure
B) To serialize Java objects to JSON
C) To manage HTTP connections
D) To format dates in JSON
Answer:
A) To represent a node in a JSON tree structure
Explanation:
The JsonNode
class in Jackson is used to represent a node in a JSON tree structure. It provides a way to traverse, manipulate, and inspect JSON data programmatically. JsonNode
allows you to work with JSON objects, arrays, and values in a flexible and dynamic way, making it ideal for scenarios where the structure of the JSON data is not known in advance.
For example:
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(jsonString);
String name = rootNode.get("name").asText();
In this example, JsonNode
is used to parse a JSON string into a tree structure, allowing the code to extract the value of the “name” property.