概述
TreeNode
接口,JsonNode
抽象类实现 TreeNode
主要关注 TreeNode
接口的方法
ObjectMapper 相关方法
-
readTree
-
writeTree
简单使用
String user = objectMapper.writeValueAsString(User.newDemoUser());
JsonNode userJsonNode = objectMapper.readTree(user);
System.out.println(userJsonNode); // {"name":"张三","age":18,"birthDate":949494020000,"likes":["sing","dance","read"]}
JsonNode birthDateJsonNode = userJsonNode.get("birthDate");
System.out.println(birthDateJsonNode); // 949494020000
objectMapper.writeTree(objectMapper.createGenerator(System.out), userJsonNode);
// {"name":"张三","age":18,"birthDate":949494020000,"likes":["sing","dance","read"]}
TreeNode
方法
Minimal introspection methods
-
asToken
-
numberType
-
size
判断节点类型,只有一个返回 true
-
isValueNode
-
isContainerNode
-
isMissingNode
以下两个方法任何一个为 true 时,isContainerNode
为 true
-
isArray
-
isObject
Basic traversal through structured entries (Arrays, Objects)
-
get
-
path
-
fieldNames
-
at
Converting to/from Streaming API
traverse
JsonNode
方法
Public API, type introspection
- getNodeType
-
isPojo
-
isNumber
-
isIntegralNumber
-
isFloatingPointNumber
-
isShort
-
isInt
-
isLong
-
isFloat
-
isDouble
-
isBigDecimal
-
isBigInteger
-
isTextual
-
isBoolean
-
isNull
-
isBinary
-
canConvertToLong
-
canConvertToExactIntegral
Public API, straight value access
-
textValue
-
binaryValue
-
booleanValue
-
numberValue
-
shortValue
-
intValue
-
longValue
-
floatValue
-
doubleValue
-
decimalValue
-
bigIntegerValue
Public API, value access with conversion(s)/coercion(s)
-
asText
-
asInt
-
asLong
-
asDouble
-
asBoolean
Public API, extended traversal (2.10) with "required()"
-
require
-
requireNonNull
-
required
-
requiredAt
Public API, value find / existence check methods
-
has
-
hasNonNull
Public API, container access
-
iterator
-
elements
-
fields
Public API, find methods
-
findValue
-
findValues
-
findValuesAsText
-
findPath
-
findParent
-
findParents
Public API, path handling
-
with
-
withArray
Other
- toPrettyString
常用方法 findValue
String user = objectMapper.writeValueAsString(User.newDemoUser());
JsonNode userJsonNode = objectMapper.readTree(user);
System.out.println(
userJsonNode); // {"name":"张三","age":18,"birthDate":949494020000,"likes":["sing","dance","read"]}
JsonNode birthDateJsonNode = userJsonNode.get("birthDate");
System.out.println(birthDateJsonNode); // 949494020000
objectMapper.writeTree(objectMapper.createGenerator(System.out), userJsonNode);
// {"name":"张三","age":18,"birthDate":949494020000,"likes":["sing","dance","read"]}
JSON Pointer
相关方法:
- at
ClassPathResource classPathResource = new ClassPathResource("test.json");
JsonNode userJsonNode = objectMapper.readTree(classPathResource.getReader(StandardCharsets.UTF_8));
JsonNode friendsJsonNode = userJsonNode.at("/0/friends");
Console.log(friendsJsonNode.toString()); // [{"id":0,"name":"Bentley Randolph"},{"id":1,"name":"Kirkland Hinton"},{"id":2,"name":"Darlene Riley"}]
JsonNode nameJsonNode = friendsJsonNode.at("/1/name");
Console.log(nameJsonNode); // Kirkland Hinton
标签:JsonNode,name,20221117,模型,API,Public,objectMapper,userJsonNode
From: https://www.cnblogs.com/huangwenjie/p/16911724.html