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 结构中)
}
相关推荐
Java Fans5 小时前
WPF使用SQLite与JSON文本文件结合存储体侧平衡数据的设计与实现
sqlite·json·wpf
还不如ctrC+V8 小时前
VScode在 Markdown 编辑器中预览
node.js·json
一个天蝎座 白勺 程序猿16 小时前
Python爬虫(8)Python数据存储实战:JSON文件读写与复杂结构化数据处理指南
爬虫·python·json
一路向北he16 小时前
pcm数据不支持存储在json里面,需要先转base64
json·pcm
沉迷...3 天前
详解.vscode 下的json .vscode文件夹下各个文件的作用
ide·vscode·json
聪明的墨菲特i3 天前
SQL进阶知识:九、高级数据类型
xml·数据库·sql·mysql·json·空间数据类型
AAA顶置摸鱼3 天前
使用 Pandas 进行多格式数据整合:从 Excel、JSON 到 HTML 的处理实战
json·excel·pandas
冰^3 天前
MySQL VS SQL Server:优缺点全解析
数据库·数据仓库·redis·sql·mysql·json·数据库开发
Jamesvalley3 天前
【修复】Django收到请求报Json解析错误
django·json
异常君4 天前
Java 序列化工具:@JSONField 注解实战解析与应用技巧
java·后端·json