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 结构中)
}
相关推荐
远方16091 天前
16-Oracle 23 ai-JSON-Relational Duality-知识准备
数据库·oracle·json
sc写算法1 天前
基于nlohmann/json 实现 从C++对象转换成JSON数据格式
开发语言·c++·json
酷爱码1 天前
Spring Boot项目中JSON解析库的深度解析与应用实践
spring boot·后端·json
不惑_2 天前
用 PyQt5 打造一个可视化 JSON 数据解析工具
开发语言·qt·json
漫游者Nova2 天前
PDF转Markdown/JSON软件MinerU最新1.3.12版整合包下载
pdf·json·markdown·mineru
snow@li2 天前
vue3+ts+vite:详细、完整的 tsconfig.json 示例 / 常见配置项及其用途
json·tsconfig.json
南郁2 天前
007-nlohmann/json 项目应用-C++开源库108杰
c++·开源·json·nlohmann·现代c++·d2school·108杰
紫乾20143 天前
idea json生成实体类
java·json·intellij-idea
愿你天黑有灯下雨有伞4 天前
MyBatis-Plus LambdaQuery 高级用法:JSON 路径查询与条件拼接的全场景解析
mysql·json·mybatis
wtsolutions4 天前
JSON to Excel 3.0.0 版本发布 - 从Excel插件到Web应用的转变
json·excel·json-to-excel·wtsolutions