tags: Jackson JsonNode ObjectNode
Jackson JsonNode class, the full path com.fasterxml.jackson.databind.JsonNode, is Jackson's json tree model (FIG Object Model). Jackson can read JSON to JsonNode example, write JsonNode to JSON. This article does not involve json serialization or deserialization, describes how to build from scratch JsonNode object graph, you can then serialized to json.
The Jackson JsonNode objects immutable, which means you can not build direct object graph JsonNode instance, but you can create JsonNode object graph subclass ObjectNode instance. As a subclass of JsonNode, ObjectNode JsonNode can be used in any place. You'll see later how to build ObjectNode object graph.
In order to use Jackson read json as JsonNode, we need to create Jackson ObjectMapper instance. Then call its readTree () method, using as a parameter the source json, Below is an example:
String json = "{ \"f1\" : \"v1\" } ";
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(json);
System.out.println(jsonNode.get("f1").asText());
The actual project ObjectMapper should always create such a container is injected from the spring.
Jackson wrote JsonNode to use json, also need ObjectMapper object. Call writeValueAsString () method, you write or other suitable method (writeValue), see example:
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = readJsonIntoJsonNode();
String json = objectMapper.writeValueAsString(jsonNode);
readJsonIntoJsonNode () method is the method I created json parse the string value JsonNode for, in order to generate JsonNode to write. The method to extract content in this case is not the point, you only need to know to produce a JsonNode object. It is important to call the writeValueAsString ObjectWriter's () method JsonNode write JSON string. ObjectMapper provides many write method, used for different purposes writes, readers can view the source code or documentation.
And json objects, like, JsonNode may be more than one field. Suppose we parse the following json value JsonNode:
{
"field1" : "value1",
"field2" : 999
}
json has two fields, if you indicate above json object with the jsonNode, you can get its two fields:
JsonNode jsonNode = ... //parse above JSON into a JsonNode
JsonNode field1 = jsonNode.get("field1");
JsonNode field2 = jsonNode.get("field2");
Note that, even if the two fields are of type string, but always get method return type field indicating JsonNode.
Jackson JsonNode has a special method at () method. The method can be given at the root node JsonNode Json object graph to access any field.
examples are as follows:
{
"identification" : {
"name" : "James",
"ssn: "ABC123552"
}
}
If the corresponding JsonNode json name field which can be accessed by at methods:
JsonNode nameNode = jsonNode.at("/identification/name");
Note that at the method parameters: string "/ identification / name", which is json path expressions. Path expression specifies the complete path from the root to access the field to JsonNode, the path to the file system is very similar. But need to be reminded that must begin with /.
at JsonNode method returns the fields to be accessed, if not found null is returned. In order to obtain the actual value of the field, you need to call other methods of conversion, the following section began to explain.
Jackson JsonNode class provides a set of methods can be converted to another data type field value. As long, string and the like. Below is an example:
String f2Str = jsonNode.get("f2").asText();
double f2Dbl = jsonNode.get("f2").asDouble();
int f2Int = jsonNode.get("f2").asInt();
long f2Lng = jsonNode.get("f2").asLong();
If the field contains the value 123456 f2, it is possible to convert the pictures to a string, double, int, long type.
Mentioned earlier, JsonNode is immutable. To create JsonNode FIG objects, you need to change JsonNode FIG example, as set attribute values and sub JsonNode examples. Because of its immutability, can not operate directly, an alternative is subclass ObjectNode, will be described in detail below.
First create ObjectNode instance:
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode objectNode = objectMapper.createObjectNode();
In order to set ObjectNode object properties, call the set method. Incoming and JsonNode field names as parameters.
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode parentNode = objectMapper.createObjectNode();
JsonNode childNode = readJsonIntoJsonNode();
parentNode.set("child1", childNode);
readJsonIntoJsonNode () method is responsible for producing some of the JsonNode objects, we are ready as a child node of ObjectNode.
ObjectNode class also provides a method for setting a set value of the original data type attribute. Conversion with respect to the original set value JsonNode then simpler. Below is an example:
objectNode.put("field1", "value1");
objectNode.put("field2", 123);
objectNode.put("field3", 999.999);
This article describes the JsonNode Jackson and ObjectNode two classes, the former is immutable, generally used for reading. The latter variable, is generally used to create objects Json FIG.
When using Jackson today, when converting a json string into a json object What I got is a JsonNode object, and then found that this object cannot be put and add operations Now provide the following s...
1 Overview Use JSONNODE to make a variety of conversions and add, modify, and delete nodes. 2. Create a node The first step in creating a node is to instantiate the ObjectMapper object using the defau...
Can you directly convert Java objects to JSONNODE objects? The only way to solve this problem is to convert the Java object to String and then convert to jsonnode: Example...
When the JSON library from org.json changed to Jackson, I hope to use Jackson to reproduce the following code: But Jackson in JsonNode.get (field) method returns or a JsonNode, but datasets actually a...
Overview The main purpose of this fast guide is how to use Jackson 2 to convert a string to JSONNODE object. JSONNODE definitioncom.fasterxml.jackson.databind.JsonNode in the bag. Fast conversion You ...
The introduction is dependent: Maven Warehouse Address: https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind...
[url]http://jackson.codehaus.org/[/url] lib: jackson-core-asl-1.6.0.jar jackson-mapper-asl-1.6.0.jar [b]Experience: The most convenient way is the Full Data Binding mode, no extra processing, but the ...
1 Jackson Overview Serialization and deserialization of json's Java open source framework Spring MVC's default json parser 1.1 Advantages Jackson relies on fewer jars and is easy to use. Compared to o...
REST Assured Series summary The REST Assured 44 - Fetch Value From JSON Object Using JsonNode - Jackson - Get () & Path () Methods introduce When you need to resolve a long, nested JSON, POJO clas...