Jackson 简明教程
Jackson - Tree Model
树模型准备 JSON 文档的内存中树表示形式。ObjectMapper 构建 JsonNode 节点的树。这是最灵活的方法。它类似于 XML 的 DOM 解析器。
Tree Model prepares a in-memory tree representation of the JSON document. ObjectMapper build tree of JsonNode nodes. It is most flexible approach. It is analogus to DOM parser for XML.
Create Tree from JSON
ObjectMapper 在读取 JSON 后提供指向树的根节点的指针。根节点可用于遍历整个树。考虑以下代码片段以获取提供的 JSON 字符串的根节点。
ObjectMapper provides a pointer to root node of the tree after reading the JSON. Root Node can be used to traverse the complete tree. Consider the following code snippet to get the root node of a provided JSON String.
//Create an ObjectMapper instance
ObjectMapper mapper = new ObjectMapper();
String jsonString = "{\"name\":\"Mahesh Kumar\", \"age\":21,\"verified\":false,\"marks\": [100,90,85]}";
//create tree from JSON
JsonNode rootNode = mapper.readTree(jsonString);
Traversing Tree Model
遍历树并获取数据时,使用到达根节点的相对路径获取每个节点。考虑以下代码片段,遍历树并提供根节点。
Get each node using relative path to the root node while traversing tree and process the data. Consider the following code snippet traversing the tree provided the root node.
JsonNode nameNode = rootNode.path("name");
System.out.println("Name: "+ nameNode.textValue());
JsonNode marksNode = rootNode.path("marks");
Iterator<JsonNode> iterator = marksNode.elements();
Example
在 C:>Jackson_WORKSPACE 中创建一个名为 JacksonTester 的 java 类文件。
Create a java class file named JacksonTester in C:\>Jackson_WORKSPACE.
文件:JacksonTester.java
File: JacksonTester.java
import java.io.IOException;
import java.util.Iterator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonTester {
public static void main(String args[]){
try {
ObjectMapper mapper = new ObjectMapper();
String jsonString = "{\"name\":\"Mahesh Kumar\", \"age\":21,\"verified\":false,\"marks\": [100,90,85]}";
JsonNode rootNode = mapper.readTree(jsonString);
JsonNode nameNode = rootNode.path("name");
System.out.println("Name: "+ nameNode.textValue());
JsonNode ageNode = rootNode.path("age");
System.out.println("Age: " + ageNode.intValue());
JsonNode verifiedNode = rootNode.path("verified");
System.out.println("Verified: " + (verifiedNode.booleanValue() ? "Yes":"No"));
JsonNode marksNode = rootNode.path("marks");
Iterator<JsonNode> iterator = marksNode.elements();
System.out.print("Marks: [ ");
while (iterator.hasNext()) {
JsonNode marks = iterator.next();
System.out.print(marks.intValue() + " ");
}
System.out.println("]");
}
catch (JsonParseException e) { e.printStackTrace(); }
catch (JsonMappingException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
}
}
Verify the result
Verify the result
使用 javac 编译器编译类,如下所示:
Compile the classes using javac compiler as follows:
C:\Jackson_WORKSPACE>javac JacksonTester.java
现在运行 jacksonTester 查看结果:
Now run the jacksonTester to see the result:
C:\Jackson_WORKSPACE>java JacksonTester
验证输出
Verify the Output
Name: Mahesh Kumar
Age: 21
Verified: No
Marks: [ 100 90 85 ]