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 结构中)
}
相关推荐
Zwb2997921 天前
Day 30 - 错误、异常与 JSON 数据 - Python学习笔记
笔记·python·学习·json
眠りたいです1 天前
基于脚手架微服务的视频点播系统-脚手架开发部分-jsoncpp,protobuf,Cpp-httplib与WebSocketpp中间件介绍与使用
c++·websocket·微服务·中间件·json·protobuf·cpp-httplib
我是大头鸟2 天前
XMLHttpRequest 发送json 格式的数据,servlet 接收
servlet·json·xmlhttprequest
程序新视界3 天前
一篇文章详解你不知道的MySQL JSON数据类型
数据库·mysql·json
数据知道3 天前
Go基础:json文件处理详解(标准库`encoding/json`)
开发语言·后端·golang·json·go语言
广都--编程每日问4 天前
deepseek 的对话json导出成word和pdf
pdf·json·word·deepseek·exprot
包达叔5 天前
仿NewLife的XmlConfig类实现Json配置文件
c#·json·newlife
爱吃香蕉的阿豪7 天前
.NET Core 中 System.Text.Json 与 Newtonsoft.Json 深度对比:用法、性能与场景选型
数据库·json·.netcore
千叶寻-7 天前
package.json详解
前端·vue.js·react.js·webpack·前端框架·node.js·json
睿麒8 天前
鸿蒙app开发中 拿到json文件数据进行动画的播放
json