Jackson

Jackson

xml 复制代码
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson}</version>
</dependency>

Json 读取有两种模式:

  1. JSON Tree Model
    1. 树的根代表整个JSON文档。
    2. 树中的每个节点对应于JSON值,如对象、数组、字符串、数字、布尔值或空值。
    3. 节点可以有子节点,表示嵌套的JSON结构。
  2. Data-binding method
    1. 直接转换成对象
java 复制代码
try {
    ObjectMapper mapper = new ObjectMapper();
    // tree model
    JsonNode rootNode = mapper.readTree(json);
    JsonNode appleNode = rootNode.path("a").path("b").path("c");
    // data-binding
    Apple apple = mapper.treeToValue(appleNode, Apple.class);
} catch (Exception e) {
    e.printStackTrace();
}

JSON Tree Model

  1. readTree
  2. path
  3. findValue
  4. findPath
  5. asText
  6. isEmpty
  7. isArray
  8. iterator (foreach)
  9. treeToValue

Data-binding method (注解方法)

java 复制代码
@Data
@JacksonXmlRootElement(localName = "xBody")
public class Body {
    @JacksonXmlProperty(isAttribute = true)
    private String     version;
    @JacksonXmlProperty(localName = "total")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Integer    total;
    @JacksonXmlProperty(localName = "item")
    @JsonProperty("item")
    @JacksonXmlElementWrapper(localName = "list")
    private List<Item> itemList;
    @JacksonXmlProperty(localName = "order")
    @JacksonXmlElementWrapper(useWrapping = false)
    private List<Order> orders;

    @Data
    public static class Item {
	    @JacksonXmlProperty
		private String name;
	}
}
  • 注解簇

Spring 自带 Json 工厂

JsonNode 转 Map

使用 springboot 提供的 json 转换工具 org.springframework.boot.json.JsonParserFactory

java 复制代码
private static Map<String, Object> setAllValue(JsonNode source, BillSubData target) {

    JsonParser jsonParser = JsonParserFactory.getJsonParser();

    Map<String, Object> sourceMap = jsonParser.parseMap(source.toString());
    for (Map.Entry<String, Object> entry : sourceMap.entrySet()) {
        target.set(entry.getKey(), entry.getValue());
    }
    return sourceMap;
}

JsonNode 判断 null

java 复制代码
// Method similar to #findValue(str), 
// but that will return a "missing node" instead of null if no field is found. 
// Missing node is a specific kind of node for which isMissingNode returns true;
JsonNode node = jsonNode.findPath("key"); // 必须使用 findPath 获取 JsonNode 对象

if (node.isNull()) {
    // 节点为空
}
if (node.isEmpty()) {
    // 节点为空(null、空对象、空数组)
}
if (node.isMissingNode()) {
    // 是否为缺失节点(不存在于 JSON 结构中)
}
相关推荐
小袁拒绝摆烂2 小时前
多表关联大平层转JSON树形结构
java·json
学术阿凡提7 小时前
Spring Boot 集成 Fastjson2 完整教程:从入门到避坑
spring boot·安全·json
LIUAWEIO7 小时前
鸽鸽工具网:免费在线工具大全,打开网页即用
人工智能·安全·ai·json
半天法师13 小时前
Bug 记录:UE 结构体转 JSON 时 Key 字段大小写异常 (Editor 与打包后表现不一致)
ai·ue5·json·bug
鸽芷咕13 小时前
KingbaseES数据类型完全指南:从基础CHAR到JSON/XML/几何类型
xml·oracle·json
归途醉染1 天前
Swifter.Json
c#·json·swifter.json
网络点点滴1 天前
NPM 和 package.json 文件简介
前端·npm·json
夜瞬2 天前
HTTP基础教程:请求方法、状态码、JSON、鉴权、超时、重试与流式返回
网络协议·http·json
wtsolutions2 天前
JSON-to-Excel 本地化应用发布:安全离线转换,数据零泄露
安全·json·excel
剑神一笑3 天前
从 JSON.parse 到树形视图:实现一个在线 JSON 格式化工具
前端·javascript·json