com.fasterxml.jackson.databind.JsonNode

Base class for all JSON nodes, which form the basis of JSON Tree Model that Jackson implements. One way to think of these nodes is to consider them similar to DOM nodes in XML DOM trees.

#所有JSON节点的基类,它们构成了Jackson实现的JSON树模型的基础。 考虑这些节点的一种方法是考虑它们类似于XML DOM树中的DOM节点。

As a general design rule, most accessors ("getters") are included in this base class, to allow for traversing structure without type casts. Most mutators, however, need to be accessed through specific sub-classes (such as ObjectNode and ArrayNode). This seems sensible because proper type information is generally available when building or modifying trees, but less often when reading a tree (newly built from parsed JSON content).

#作为一般的设计规则,大多数访问器(“getters”)都包含在这个基类中,以允许在没有类型转换的情况下遍历结构。 然而,大多数诱变器需要通过特定的子类(如对象节点和数组节点)访问)。 这似乎是明智的,因为在构建或修改树时通常可以获得适当的类型信息,但在读取树时(从解析的JSON内容新建)则较少)。

Actual concrete sub-classes can be found from package com.fasterxml.jackson.databind.node.

Note that it is possible to "read" from nodes, using method TreeNode.traverse(ObjectCodec), which will result in a JsonParser being constructed. This can be used for (relatively) efficient conversations between different representations; and it is what core databind uses for methods like ObjectMapper.treeToValue(TreeNode, Class) and ObjectMapper.treeAsTokens(TreeNode)

请注意,可以使用方法Tree Node.Traverse(Object Codec)从节点“读取”,这将导致构建Json Parser。 这可以用于不同表示之间(相对)有效的对话;它是核心数据库用于对象映射器.tree to Value(Tree Node,Class)和对象Mapper.tree As Token(Tree Node)等方法的内容)

例如:

  • 测试Json字符串

{
    "body": {
        "data": {
            "testId": "6453523578877563"
        },
        "resultCode": "010",
        "resultMsg": "操作成功"
    },
    "header": {
        "testserver": "10.168.14.99:8088",
        "timestamp": "1609385201558",
        "tn": "1234567"
    }
}
  • 测试代码:
package com.test;

import java.io.IOException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class TestJsonNode {
	public static void main(String[] args) {
		//准备测试Json字符串
		String jsonString ="{"
    +"\"body\": {"
    +"  \"data\": {"
    +"        \"testId\": \"6453523578877563\" " +"},"
    +"    \"resultCode\": \"010\","
    +"    \"resultMsg\": \"操作成功\""
    +"},"
    +"\"header\": {"
    +"\"testserver\":\"10.168.14.99:8088\","
    +"    \"tn\": \"1234567\","
    +"    \"timestamp\": \"1609385201558\""
    +"}"
    +"}"
				+ "";
	System.out.println(jsonString);
	ObjectMapper mapper = new ObjectMapper();
	try {
		JsonNode treeNode = mapper.readTree(jsonString);
		JsonNode bodyNode = treeNode.get("body");
		//body = {"data":{"testId":"6453523578877563"},"resultCode":"010","resultMsg":"操作成功"}
		JsonNode headerNode = treeNode.get("header");
		//header = {"testserver":"10.168.14.99:8088","tn":"1234567","timestamp":"1609385201558"}
		JsonNode dataNode = bodyNode.get("data");
		if (null != dataNode && dataNode.size() > 0) {
			System.out.println("data testId="+dataNode.get("testId"));
			System.out.println("data resultCode="+dataNode.get("resultCode"));
			System.out.println("data resultMsg="+dataNode.get("resultMsg"));
		}
		System.out.println("header nodes:");
		if (null != headerNode && headerNode.size() > 0) {
			System.out.println("header testserver="+headerNode.get("testserver"));
			System.out.println("header tn="+headerNode.get("tn"));
			System.out.println("header timestamp="+headerNode.get("timestamp"));
		}
	} catch (IOException e) {
		e.printStackTrace();
	}
	}
}
  • 测试结果:

{"body": {  "data": {        "testId": "6453523578877563" },    "resultCode": "010",    "resultMsg": "操作成功"},"header": {"testserver":"10.168.14.99:8088",    "tn": "1234567",    "timestamp": "1609385201558"}}
data testId="6453523578877563"
data resultCode=null
data resultMsg=null
header nodes:
header testserver="10.168.14.99:8088"
header tn="1234567"
header timestamp="1609385201558"

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值