Java处理JSON技巧教学(从基础到高阶实战全覆盖)

在当下后端开发、接口交互、数据存储、微服务通信场景中,JSON已然成为最核心、最通用的数据交换格式。相较于XML,JSON具备轻量化、语法简洁、解析速度快、可读性高、适配前后端交互等优势,是Java开发者日常开发中必须熟练掌握的核心技能。

绝大多数Java业务Bug、接口异常、数据解析错误,都源于JSON序列化、反序列化的不规范操作、框架使用不当、特殊场景处理缺失。本文将从零起步,系统讲解Java处理JSON的基础语法、主流框架、核心注解、实战技巧、避坑方案、性能优化、高阶场景解决方案,覆盖新手入门到企业级高阶开发全场景,帮助开发者彻底吃透Java JSON处理核心技术,规避90%以上的JSON解析问题。

一、JSON核心基础认知(必学前置知识)

1.1 JSON定义与核心特性

JSON(JavaScript Object Notation,JavaScript对象标记语言)是一种独立于编程语言的轻量级文本数据格式,虽衍生自JavaScript,但目前已被所有主流编程语言兼容,是跨语言、跨端数据交互的标准格式。

核心特性:

  • 轻量化:文本体积小,传输、解析效率远超XML;

  • 结构清晰:仅包含对象、数组两种核心结构,语法极简;

  • 类型严格:支持字符串、数字、布尔、null、对象、数组6种数据类型,无多余冗余类型;

  • 跨平台兼容:前后端、微服务、第三方接口交互通用,无语言壁垒。

1.2 JSON标准语法规范(开发必备)

JSON语法严格区分大小写、标点符号,格式错误会直接导致解析失败,核心语法规则如下:

  • JSON对象:以{}包裹,由键值对 组成,键必须是双引号包裹的字符串,禁止单引号、无引号;

  • JSON数组:以[]包裹,内部可存放任意合法JSON数据类型,元素用逗号分隔;

  • 数据类型:字符串(双引号)、数字(整数/浮点数)、布尔(true/false小写)、null(小写)、对象、数组;

  • 禁止语法:禁止使用注释、禁止键名不写引号、禁止末尾多余逗号、禁止单引号包裹字符串。

标准JSON示例:

复制代码

{ "username": "张三", "age": 25, "isVip": true, "hobbies": ["编程", "阅读"], "address": { "province": "广东省", "city": "深圳市" }, "remark": null }

1.3 Java处理JSON的核心场景

日常开发中,Java操作JSON的核心需求只有两类,所有复杂场景均基于这两类需求延伸:

  1. 序列化:Java对象 → JSON字符串(接口返回数据、缓存存储、消息队列传输);

  2. 反序列化:JSON字符串 → Java对象(接收前端参数、解析第三方接口数据、读取JSON配置文件)。

Java原生无内置JSON解析工具,必须依赖第三方开源框架,目前企业主流框架分为三类:Jackson、Gson、Fastjson,下文将逐一深度解析。

二、主流JSON框架对比与选型指南

目前Java生态中主流JSON处理框架为Jackson、Gson、Fastjson,三者各有优劣,不同业务场景适配不同框架,精准选型是高效开发的前提。

框架 核心优势 缺点 适用场景
Jackson Spring生态默认集成、性能优异、注解丰富、稳定性高、支持复杂场景、持续维护更新 基础使用稍繁琐,原生不支持优雅的泛型解析 SpringBoot/SpringCloud项目、企业级正式项目、高并发场景
Gson Google开源、API简洁、上手简单、泛型解析友好、兼容性强 注解功能较弱、自定义序列化拓展性差、性能略低于Jackson 小型项目、工具类开发、Android开发、简单JSON解析场景
Fastjson2 API极简、序列化速度极快、自动适配类型、零配置上手 旧版本漏洞多、社区迭代波动、Spring生态适配性一般 高性能数据解析、内部工具项目、批量数据处理场景(推荐Fastjson2)

企业选型结论 :90%的Java后端项目首选Jackson(Spring默认集成,无需额外引入依赖,稳定性、兼容性、拓展性最优);简单工具类、Android项目选用Gson;超高吞吐批量数据处理选用Fastjson2(规避旧版本Fastjson安全漏洞)。

三、Jackson框架全解(Spring生态首选,核心重点)

Jackson是Spring Boot、Spring Cloud生态默认内置的JSON处理框架,无需手动排除默认配置,是企业开发的标准选型。它支持精细化序列化控制、日期格式化、空值处理、字段忽略、自定义转换器等全套功能,适配所有复杂业务场景。

3.1 环境依赖引入

3.1.1 SpringBoot项目(无需手动引入)

SpringBoot的spring-boot-starter-web依赖已自动集成Jackson核心包(jackson-databind、jackson-core、jackson-annotations),直接开箱即用。

3.1.2 普通Maven项目手动引入
复制代码

<!-- Jackson核心依赖 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.2</version> </dependency> <!-- 适配Java8+日期时间类型 --> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> <version>2.15.2</version> </dependency>

3.2 核心基础用法(序列化+反序列化)

Jackson核心操作类为ObjectMapper,所有JSON转换操作均通过该类实现。

3.2.1 实体类准备
复制代码

import lombok.Data; import java.time.LocalDateTime; @Data public class User { // 用户名 private String username; // 年龄 private Integer age; // 注册时间 private LocalDateTime registerTime; // 个人简介(可为空) private String remark; }

3.2.2 序列化:对象转JSON字符串
复制代码

import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import java.time.LocalDateTime; public class JacksonDemo { public static void main(String[] args) throws Exception { // 1. 创建ObjectMapper对象(全局建议单例) ObjectMapper objectMapper = new ObjectMapper(); // 注册Java8时间模块,支持LocalDateTime序列化 objectMapper.registerModule(new JavaTimeModule()); // 2. 构建实体对象 User user = new User(); user.setUsername("张三"); user.setAge(25); user.setRegisterTime(LocalDateTime.now()); // 3. 对象转JSON字符串 String jsonStr = objectMapper.writeValueAsString(user); System.out.println("序列化结果:" + jsonStr); } }

3.2.3 反序列化:JSON字符串转对象
复制代码

// JSON字符串 String jsonStr = "{\"username\":\"张三\",\"age\":25,\"registerTime\":\"2026-06-10T15:30:00\"}"; // 反序列化为实体对象 User user = objectMapper.readValue(jsonStr, User.class); System.out.println("反序列化用户名:" + user.getUsername());

3.2.4 集合、泛型反序列化(高频避坑点)

直接使用List.class无法解析泛型,会导致泛型擦除,必须使用TypeReference指定泛型类型,这是Jackson最核心的基础技巧。

复制代码

// JSON数组字符串 String jsonArray = "[{\"username\":\"张三\",\"age\":25},{\"username\":\"李四\",\"age\":28}]"; // 泛型集合反序列化,解决泛型擦除问题 List<User> userList = objectMapper.readValue(jsonArray, new TypeReference<List<User>>() {}); userList.forEach(System.out::println);

3.3 核心注解实战技巧(精细化控制JSON转换)

Jackson的核心价值在于注解精细化控制序列化规则,解决字段映射、空值、日期、忽略字段、别名适配等业务问题,以下是开发高频必用注解。

3.3.1 @JsonProperty:字段别名映射

适配前后端字段命名差异(后端驼峰、前端下划线),自定义JSON键名。

复制代码

@Data public class User { // 序列化后字段名为user_name,而非userName @JsonProperty("user_name") private String userName; }

3.3.2 @JsonIgnore:忽略指定字段

序列化、反序列化时自动忽略该字段,适用于密码、密钥、冗余字段等敏感/无需返回的字段。

复制代码

@Data public class User { private String username; // 序列化时忽略密码字段,不返回前端 @JsonIgnore private String password; }

3.3.3 @JsonFormat:日期格式化

解决日期时间序列化默认格式怪异问题,自定义统一日期格式与时区。

复制代码

@Data public class User { // 统一日期格式化,指定时区避免时差问题 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private LocalDateTime registerTime; }

3.3.4 @JsonInclude:空值过滤

过滤null、空字符串、空集合等空值字段,精简返回JSON体积,优化接口响应数据。

复制代码

// 类上注解:所有空值字段不参与序列化 @JsonInclude(JsonInclude.Include.NON_NULL) @Data public class User { private String username; private String remark; // 为null时,不返回该字段 }

3.3.5 @JsonIgnoreProperties:忽略未知字段

解决前端传参多余字段、第三方接口返回多余字段导致的解析报错问题,企业开发必加。

复制代码

// 忽略JSON中所有未知字段,避免解析异常 @JsonIgnoreProperties(ignoreUnknown = true) @Data public class User { private String username; private Integer age; }

3.4 ObjectMapper全局配置技巧

单次注解配置仅针对单个实体类,通过ObjectMapper全局配置,可实现项目统一JSON解析规则,无需重复注解。

复制代码

import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; // 全局统一ObjectMapper配置(项目单例) public class JsonConfig { public static ObjectMapper getObjectMapper() { ObjectMapper objectMapper = new ObjectMapper(); // 1. 忽略未知字段,全局适配多余参数 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // 2. 序列化空对象不报错 objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); // 3. 日期全局统一格式化 JavaTimeModule javaTimeModule = new JavaTimeModule(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter)); javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter)); objectMapper.registerModule(javaTimeModule); // 4. 全局过滤null空值 objectMapper.setSerializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL); return objectMapper; } }

四、Gson框架实战技巧(简洁轻量首选)

Gson是Google开源的轻量级JSON框架,API极简、上手零门槛,泛型解析天然友好,无复杂配置,适合小型项目、工具类开发及Android开发。相较于Jackson,Gson无需处理复杂的模块注册,基础操作更加简洁。

4.1 Maven依赖引入

复制代码

<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.10.1</version> </dependency>

4.2 基础序列化与反序列化

Gson核心操作类为Gson,全局建议单例创建,避免频繁实例化浪费性能。

复制代码

import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.util.List; public class GsonDemo { // 全局单例Gson对象 private static final Gson GSON = new Gson(); public static void main(String[] args) { // 1. 对象序列化 User user = new User(); user.setUsername("李四"); user.setAge(26); String jsonStr = GSON.toJson(user); System.out.println("Gson序列化:" + jsonStr); // 2. 普通对象反序列化 User parseUser = GSON.fromJson(jsonStr, User.class); // 3. 泛型集合反序列化(Gson原生友好,无需复杂工具类) String jsonArray = "[{\"username\":\"张三\",\"age\":25}]"; List<User> userList = GSON.fromJson(jsonArray, new TypeToken<List<User>>(){}.getType()); } }

4.3 Gson核心实用技巧

4.3.1 自定义配置Gson实例

通过GsonBuilder自定义序列化规则,实现格式化输出、日期格式化、空值保留等功能。

复制代码

Gson gson = new GsonBuilder() .setPrettyPrinting() // 格式化输出JSON(便于调试) .setDateFormat("yyyy-MM-dd HH:mm:ss") // 全局日期格式化 .serializeNulls() // 保留null字段(默认不保留) .create();

4.3.2 Gson常用注解
  • @SerializedName("xxx"):字段别名,适配前后端字段差异,对应Jackson的@JsonProperty

  • @Expose:指定字段是否参与序列化/反序列化,精准控制字段暴露范围。

复制代码

import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; @Data public class User { // 序列化别名 @SerializedName("user_name") private String userName; // 不参与序列化,仅参与反序列化 @Expose(serialize = false, deserialize = true) private String password; }

五、Fastjson2高性能实战技巧(高吞吐场景首选)

Fastjson是阿里开源的高性能JSON框架,Fastjson2修复了旧版本所有安全漏洞,序列化速度优于Jackson和Gson,API极简、零配置开箱即用,适合批量数据处理、高吞吐接口、日志解析等高性能场景。

5.1 Maven依赖(务必使用Fastjson2)

复制代码

<dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>2.0.48</version> </dependency>

5.2 核心基础用法

Fastjson2核心工具类为JSON,所有静态方法直接调用,无需实例化对象,使用极简。

复制代码

import com.alibaba.fastjson2.JSON; import java.util.List; public class FastjsonDemo { public static void main(String[] args) { User user = new User(); user.setUsername("王五"); user.setAge(27); // 1. 序列化:对象转JSON字符串 String jsonStr = JSON.toJSONString(user); // 2. 反序列化:JSON转对象 User parseUser = JSON.parseObject(jsonStr, User.class); // 3. 集合反序列化 String jsonArray = "[{\"username\":\"王五\",\"age\":27}]"; List<User> userList = JSON.parseArray(jsonArray, User.class); } }

5.3 Fastjson2核心注解与技巧

5.3.1 常用注解
  • @JSONField(name = "xxx"):字段别名、日期格式化;

  • @JSONField(serialize = false):忽略序列化字段。

复制代码

import com.alibaba.fastjson2.annotation.JSONField; import java.time.LocalDateTime; @Data public class User { @JSONField(name = "user_name") private String userName; @JSONField(format = "yyyy-MM-dd HH:mm:ss") private LocalDateTime registerTime; @JSONField(serialize = false) private String password; }

5.3.2 序列化特性配置
复制代码

// 格式化输出、忽略空值、日期格式化 String jsonStr = JSON.toJSONString(user, SerializerFeature.PrettyFormat, SerializerFeature.SkipNullField, SerializerFeature.WriteDateUseDateFormat);

六、Java JSON开发高频避坑指南(实战必看)

90%的JSON解析异常均来自以下高频问题,本节汇总企业开发最常见的坑,提供标准化解决方案。

6.1 泛型擦除问题

问题现象:反序列化集合、自定义泛型对象时,获取元素报类型转换异常、属性缺失。

根因:Java泛型编译期擦除,普通Class无法识别泛型参数。

解决方案 :Jackson使用TypeReference、Gson使用TypeToken、Fastjson原生支持泛型解析。

6.2 日期序列化格式异常

问题现象:Date/LocalDateTime序列化后为时间戳、默认字符串格式杂乱,前后端解析不一致。

解决方案:统一全局日期格式化配置,禁止局部零散注解,Java8+优先使用LocalDateTime+JSR310时间模块。

6.3 未知字段解析报错

问题现象:前端传参多余字段、第三方接口新增字段,导致反序列化直接抛出异常。

解决方案 :Jackson全局关闭未知字段校验、实体类添加@JsonIgnoreProperties(ignoreUnknown = true)

6.4 空值、空集合序列化冗余

问题现象:接口返回大量null、空字符串、空数组,导致响应数据臃肿、前端渲染异常。

解决方案:全局配置过滤空值字段,仅返回有效数据,精简接口响应体积。

6.5 重复创建序列化对象性能问题

问题现象:循环、接口请求中频繁new ObjectMapper()/Gson(),导致内存溢出、接口耗时飙升。

解决方案:序列化工具类全局单例,Spring项目注入Bean统一使用。

七、高阶实战技巧与场景化解决方案

7.1 自定义序列化/反序列化处理器

针对特殊业务场景(状态码转文字、数据脱敏、金额格式化),可自定义Jackson序列化处理器,实现字段个性化转换。

示例:用户手机号脱敏序列化

复制代码

// 自定义脱敏序列化器 public class PhoneDesensitizeSerializer extends StdScalarSerializer<String> { public PhoneDesensitizeSerializer() { super(String.class); } @Override public void serialize(String phone, JsonGenerator gen, SerializerProvider provider) throws IOException { if (StringUtils.isNotBlank(phone) && phone.length() == 11) { // 中间4位脱敏 phone = phone.substring(0,3) + "****" + phone.substring(7); } gen.writeString(phone); } } // 实体类使用 @Data public class User { @JsonSerialize(using = PhoneDesensitizeSerializer.class) private String phone; }

7.2 JSON动态解析(无实体类解析)

针对动态JSON结构、不确定字段场景,无需创建实体类,直接解析JSON对象、JSON数组,适配灵活数据场景。

复制代码

// Jackson动态解析 String jsonStr = "{\"username\":\"张三\",\"age\":25}"; JsonNode jsonNode = objectMapper.readTree(jsonStr); // 精准获取字段值 String username = jsonNode.get("username").asText(); Integer age = jsonNode.get("age").asInt();

7.3 批量JSON数据性能优化

处理大批量JSON数据(日志解析、批量导入、大数据同步)时,优化方案:

  1. 优先使用Fastjson2,吞吐性能最优;

  2. 序列化工具类全局单例,禁止循环创建对象;

  3. 开启Jackson/Fastjson流式解析,避免一次性加载全量数据;

  4. 过滤无用字段,减少序列化计算开销。

八、企业级JSON工具类封装(可直接复用)

整合所有优化配置,封装通用JSON工具类,统一项目解析规则,适配所有业务场景,项目可直接引入使用。

https://neimenggu.yglsylla.cn

https://fumian.yglsylla.cn

https://yanshan.yglsylla.cn

https://beipiao.yglsylla.cn

https://wensheng.yglsylla.cn

https://yglsylla.cn

https://lijiang.kaggggmm.cn

https://xingping.kaggggmm.cn

https://yanbian.kaggggmm.cn

https://8m1.kaggggmm.cn

https://7cvckkq.kaggggmm.cn

https://d57wrf.kaggggmm.cn

https://suichuan.kaggggmm.cn

https://gaoming.kaggggmm.cn

https://huadian.kaggggmm.cn

https://jingzhou.kaggggmm.cn

https://linfen.kaggggmm.cn

https://anshun.kaggggmm.cn

https://2o74.kaggggmm.cn

https://yingtan.kaggggmm.cn

https://quangang.kaggggmm.cn

https://liaozhong.kaggggmm.cn

https://yunyan.kaggggmm.cn

https://baixiang.kaggggmm.cn

https://mengyin.kaggggmm.cn

https://lindian.kaggggmm.cn

https://datong.kaggggmm.cn

https://chunhua.kaggggmm.cn

https://jinzhou.kaggggmm.cn

https://rizhao.kaggggmm.cn

https://b4cri.kaggggmm.cn

https://longling.kaggggmm.cn

https://6xtn.kaggggmm.cn

https://siping.kaggggmm.cn

https://huangchuan.kaggggmm.cn

https://yongde.kaggggmm.cn

https://leping.kaggggmm.cn

https://mile.kaggggmm.cn

https://pingyi.kaggggmm.cn

https://xinxiang.kaggggmm.cn

https://qingyuan.kaggggmm.cn

https://shiyan.kaggggmm.cn

https://cangxi.kaggggmm.cn

https://zichang.kaggggmm.cn

https://mengzi.kaggggmm.cn

https://hangzhou.kaggggmm.cn

https://ruyang.kaggggmm.cn

https://xiuwu.kaggggmm.cn

https://hedong.kaggggmm.cn

https://baishan.kaggggmm.cn

https://d2au.kaggggmm.cn

https://wuyishan.kaggggmm.cn

https://dongtai.kaggggmm.cn

https://kaggggmm.cn

https://tongzhou.ioououhb.cn

https://ruoergai.ioououhb.cn

https://yongding.ioououhb.cn

https://yuqing.ioououhb.cn

https://vnhw.ioououhb.cn

https://yanggao.ioououhb.cn

https://mjjn.ioououhb.cn

https://shuifu.ioououhb.cn

https://tekesi.ioououhb.cn

https://daiyue.ioououhb.cn

https://5zyl.ioououhb.cn

https://tahe2.ioououhb.cn

https://yizhou.ioououhb.cn

https://zhaozhou.ioououhb.cn

https://jingyuan2.ioououhb.cn

https://jiangjin.ioououhb.cn

https://p8xkji.tikqbspp.cn

https://xfpkc0x.tikqbspp.cn

https://xiangxi.tikqbspp.cn

https://donggang.tikqbspp.cn

https://zhangjiajie.tikqbspp.cn

https://changting.tikqbspp.cn

https://d6s.tikqbspp.cn

https://7sej.tikqbspp.cn

https://fengnan.tikqbspp.cn

https://cuiluan.tikqbspp.cn

https://lukou.tikqbspp.cn

https://gongyi.tikqbspp.cn

https://hequ.tikqbspp.cn

https://yantian.tikqbspp.cn

https://pinghu.tikqbspp.cn

https://yinzhou.tikqbspp.cn

https://naxi.tikqbspp.cn

https://yufeng.tikqbspp.cn

https://hengyang.tikqbspp.cn

https://21tqqot9.tikqbspp.cn

https://jiashi.tikqbspp.cn

https://yuhuan.tikqbspp.cn

https://272.tikqbspp.cn

https://laohekou.tikqbspp.cn

https://jiaoling.tikqbspp.cn

https://heishui.tikqbspp.cn

https://tikqbspp.cn

https://kzjgrla.cn

https://www_eritten_com.kzjgrla.cn

https://www_ccgrjt_com.kzjgrla.cn

https://www_yyrd_gov_cn.kzjgrla.cn

https://www_xgjlsp_com.kzjgrla.cn

https://www_unisengroup_com.kzjgrla.cn

https://www_shdongyou_com.kzjgrla.cn

https://www_zshualian_com.kzjgrla.cn

https://www_shyj188_com.kzjgrla.cn

https://www_htglue_com.kzjgrla.cn

https://www_highend-mems_com.kzjgrla.cn

https://www_welltpe_com.kzjgrla.cn

https://www_shanxihengzhi_com.kzjgrla.cn

https://nji.hxtsc.cn

https://vzt.hxtsc.cn

https://jmh.hxtsc.cn

https://heg.hxtsc.cn

https://ogo.hxtsc.cn

https://mwh.hxtsc.cn

https://hxtsc.cn

https://linqu.rcskalm.cn

https://huitong.rcskalm.cn

https://renhuai.rcskalm.cn

https://tonghai.rcskalm.cn

https://jiangshan.rcskalm.cn

https://shenzha.rcskalm.cn

https://x7f5l.rcskalm.cn

https://9el3ck7p.rcskalm.cn

https://s85upq.rcskalm.cn

https://2tptb11.rcskalm.cn

https://jiangmen.rcskalm.cn

https://huangyuan.rcskalm.cn

https://queshan.rcskalm.cn

https://jinxian.rcskalm.cn

https://shuifu.rcskalm.cn

https://gaozhou.rcskalm.cn

https://xifeng2.rcskalm.cn

https://rcskalm.cn

https://luochuan.iidqdviv.cn

https://2yvtfz82.iidqdviv.cn

https://sanhe.iidqdviv.cn

https://npysd0tw.iidqdviv.cn

https://chaoyang.iidqdviv.cn

https://guiyang.iidqdviv.cn

https://nanjiao.iidqdviv.cn

https://iy2.iidqdviv.cn

https://yanshi.iidqdviv.cn

https://qianjin.iidqdviv.cn

https://yunyang.iidqdviv.cn

https://chishui.iidqdviv.cn

https://guanshanhu.iidqdviv.cn

https://nangang.iidqdviv.cn

https://2rw.iidqdviv.cn

https://ranghulu.iidqdviv.cn

https://5y0ss.iidqdviv.cn

https://wangjiang.iidqdviv.cn

https://ucdb.iidqdviv.cn

https://bnt.iidqdviv.cn

https://sansui.iidqdviv.cn

https://haibei.iidqdviv.cn

https://t6bpheu.iidqdviv.cn

https://yanshou.iidqdviv.cn

https://lichuan.iidqdviv.cn

https://renhuai.iidqdviv.cn

https://gongqingcheng.vfobbuhz.cn

https://xigong.iidqdviv.cn

https://huilai.iidqdviv.cn

https://tongguan.iidqdviv.cn

https://fuyun.iidqdviv.cn

https://huqiu.iidqdviv.cn

https://yuanbaoshan.iidqdviv.cn

https://iidqdviv.cn

https://anguo.vfobbuhz.cn

https://iml.vfobbuhz.cn

https://huachuan.vfobbuhz.cn

https://h68mi.vfobbuhz.cn

https://gb3go.vfobbuhz.cn

https://tonghe.vfobbuhz.cn

https://7wtaf3h.vfobbuhz.cn

https://dengkou.vfobbuhz.cn

https://fenghua.vfobbuhz.cn

https://xiuzhou.vfobbuhz.cn

https://xixiangtang.vfobbuhz.cn

https://yuli.vfobbuhz.cn

https://chongzhou.vfobbuhz.cn

https://guangzhou.vfobbuhz.cn

https://fuzhou2.vfobbuhz.cn

https://meitan.vfobbuhz.cn

https://chaling.vfobbuhz.cn

https://xiangyin.vfobbuhz.cn

https://guzhen.vfobbuhz.cn

https://guannan.vfobbuhz.cn

https://songxi.vfobbuhz.cn

https://anzhou.vfobbuhz.cn

https://buerjin.vfobbuhz.cn

https://taishun.vfobbuhz.cn

https://drp0mp6.vfobbuhz.cn

https://wuhe.vfobbuhz.cn

https://anlong.vfobbuhz.cn

https://flhz.vfobbuhz.cn

https://pingba.vfobbuhz.cn

https://changdu.vfobbuhz.cn

https://queshan.vfobbuhz.cn

https://fenyang.vfobbuhz.cn

https://manasi.vfobbuhz.cn

https://y1ly20.vfobbuhz.cn

https://pingguo.vfobbuhz.cn

https://xingren.vfobbuhz.cn

https://shaowu.vfobbuhz.cn

https://wusu.vfobbuhz.cn

https://vfobbuhz.cn

https://oau.bbhhnuuu.cn

https://jingjiang.bbhhnuuu.cn

https://taijiang.bbhhnuuu.cn

https://qixing.bbhhnuuu.cn

https://tongchuan.bbhhnuuu.cn

https://ngbqmg0x.bbhhnuuu.cn

https://anhui.bbhhnuuu.cn

https://xinhua2.bbhhnuuu.cn

https://shanwei.bbhhnuuu.cn

https://hechi.bbhhnuuu.cn

https://liuzhite.bbhhnuuu.cn

https://vfx3.bbhhnuuu.cn

https://taihu.bbhhnuuu.cn

https://shuangbai.bbhhnuuu.cn

https://anxian.bbhhnuuu.cn

https://ovs2i.bbhhnuuu.cn

https://quannan.bbhhnuuu.cn

https://diaobingshan.bbhhnuuu.cn

https://fengze.bbhhnuuu.cn

https://rikaze.bbhhnuuu.cn

https://u2xhq.bbhhnuuu.cn

https://haibei.bbhhnuuu.cn

https://yucheng.bbhhnuuu.cn

https://kev2y.bbhhnuuu.cn

https://zhaotong.bbhhnuuu.cn

https://48up0iyg.bbhhnuuu.cn

https://xunyi.bbhhnuuu.cn

https://neimenggu.bbhhnuuu.cn

https://dingnan.bbhhnuuu.cn

https://adlt.bbhhnuuu.cn

https://xinganmeng.bbhhnuuu.cn

https://gucheng.bbhhnuuu.cn

https://huanghua.bbhhnuuu.cn

https://maoming.bbhhnuuu.cn

https://bbhhnuuu.cn

https://liaoyang.bbhhnuuu.cn

https://2luj90e5.bbhhnuuu.cn

https://huangyan.bbhhnuuu.cn

https://sanyuan2.bbhhnuuu.cn

https://shanghang.bbhhnuuu.cn

https://rjs1teg.bbhhnuuu.cn

https://weishan.bbhhnuuu.cn

https://4f376to.bbhhnuuu.cn

https://k4y.bbhhnuuu.cn

https://xinjiang.brexrrek.cn

https://huarong.brexrrek.cn

https://spo.brexrrek.cn

https://pinghu.brexrrek.cn

https://gucheng.brexrrek.cn

https://jinsha.brexrrek.cn

https://ziyang2.brexrrek.cn

https://liaoyuan.brexrrek.cn

https://wanrong.brexrrek.cn

https://flr1beph.brexrrek.cn

https://huangchuan.brexrrek.cn

https://litang.brexrrek.cn

https://luoyuan.brexrrek.cn

https://gidve.brexrrek.cn

https://anduo.brexrrek.cn

https://wuhou.brexrrek.cn

https://lianyungang.brexrrek.cn

https://changshu.brexrrek.cn

https://yichang.brexrrek.cn

https://s52tp.brexrrek.cn

https://wuyang.brexrrek.cn

https://taizhou.brexrrek.cn

https://songming.brexrrek.cn

https://huoqiu.brexrrek.cn

https://lianhu.brexrrek.cn

https://xingning.brexrrek.cn

https://jiande.brexrrek.cn

https://ansai.brexrrek.cn

https://0ofy.brexrrek.cn

https://foping.brexrrek.cn

https://yiyang2.brexrrek.cn

https://gaotang.brexrrek.cn

https://lean.brexrrek.cn

https://teweftvo.brexrrek.cn

https://moqbb.brexrrek.cn

https://lianghe.brexrrek.cn

https://jdkt2f.brexrrek.cn

https://hongqiao.brexrrek.cn

https://qinglong.brexrrek.cn

https://huangdao.brexrrek.cn

https://linzhang.brexrrek.cn

https://shuncheng.brexrrek.cn

https://cangxian.brexrrek.cn

https://huiyang.brexrrek.cn

https://brexrrek.cn

复制代码

import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; /** * 企业级通用JSON工具类(Jackson全局统一配置) */ public class JsonUtils { private static final ObjectMapper OBJECT_MAPPER; static { OBJECT_MAPPER = new ObjectMapper(); // 初始化时间模块 JavaTimeModule javaTimeModule = new JavaTimeModule(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter)); javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter)); OBJECT_MAPPER.registerModule(javaTimeModule); // 全局配置:忽略未知字段、过滤空值、空对象不报错 OBJECT_MAPPER.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); OBJECT_MAPPER.configure(com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS, false); OBJECT_MAPPER.setSerializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL); } /** * 对象转JSON字符串 */ public static String toJson(Object obj) { try { return OBJECT_MAPPER.writeValueAsString(obj); } catch (Exception e) { throw new RuntimeException("JSON序列化失败", e); } } /** * JSON字符串转普通对象 */ public static <T> T parseObject(String json, Class<T> clazz) { try { return OBJECT_MAPPER.readValue(json, clazz); } catch (Exception e) { throw new RuntimeException("JSON反序列化失败", e); } } /** * JSON字符串转泛型集合 */ public static <T> List<T> parseList(String json) { try { return OBJECT_MAPPER.readValue(json, new TypeReference<List<T>>() {}); } catch (Exception e) { throw new RuntimeException("JSON集合反序列化失败", e); } } }

九、总结与开发规范

本文系统讲解了Java三大主流JSON框架的基础用法、核心注解、高阶技巧、避坑方案与实战优化,覆盖从入门到企业级开发全场景。为规范开发,总结Java JSON处理企业级开发规范

  1. 框架统一:SpringBoot项目统一使用Jackson,禁止项目混用多套JSON框架,统一解析规则;

  2. 配置全局化:日期格式化、空值过滤、未知字段忽略等规则统一全局配置,禁止单个类零散配置;

  3. 工具类统一:项目封装全局JSON工具类,所有序列化、反序列化统一调用,避免重复代码;

  4. 性能优化:序列化对象全局单例,大批量数据优先Fastjson2,避免循环创建实例;

  5. 安全规范:敏感字段(密码、手机号、身份证)必须忽略或脱敏序列化,禁止明文返回;

  6. 异常处理:所有JSON解析操作必须捕获异常,避免解析失败导致接口雪崩。

熟练掌握以上技巧,可彻底解决Java开发中99%的JSON解析问题,提升代码规范性、稳定性与性能,适配日常业务开发、接口对接、大数据处理、微服务通信等所有场景。

相关推荐
李白的天不白2 小时前
使用 SmartAdmin 进行前后端开发
java·前端
swordbob2 小时前
Spring 单例 Bean 是线程安全的吗?
java·开发语言
PixelBai3 小时前
JSON差异比较实际应用场景案例
json
2601_951643773 小时前
Python第一,Java跌出前三,C语言杀回来了
java·c语言·python·编程语言排行·技术趋势
小小编程路3 小时前
C++ 异常 完整讲解
开发语言·c++
AI科技星3 小时前
数术工坊 · 第四卷 橡皮泥江湖(拓扑学)【完整定稿】
c语言·开发语言·汇编·electron·概率论·拓扑学
张忠琳4 小时前
【Go 1.26.4】Golang Select 深度解析
开发语言·后端·golang
IT 行者5 小时前
GitHub Spec Kit 实战(五):/speckit.tasks 怎么拆——Spec Kit 五部曲收官
java·ai编程·claude
AC赳赳老秦5 小时前
OpenClaw+Power Apps 实战:自动生成 Power Apps 应用、连接 Excel 数据源
大数据·开发语言·python·serverless·excel·deepseek·openclaw