惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
C
Check Point Blog
博客园 - Franky
V
Visual Studio Blog
云风的 BLOG
云风的 BLOG
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
V2EX - 技术
V2EX - 技术
AI
AI
Hacker News - Newest:
Hacker News - Newest: "LLM"
Jina AI
Jina AI
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
H
Hacker News: Front Page
H
Hackread – Cybersecurity News, Data Breaches, AI and More
O
OpenAI News
Attack and Defense Labs
Attack and Defense Labs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
爱范儿
爱范儿
H
Heimdal Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
G
Google Developers Blog
G
GRAHAM CLULEY
V
V2EX
The Register - Security
The Register - Security
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
Schneier on Security
Schneier on Security
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
Help Net Security
Help Net Security
大猫的无限游戏
大猫的无限游戏
C
CERT Recently Published Vulnerability Notes
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
S
Secure Thoughts
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
量子位
NISL@THU
NISL@THU
K
Kaspersky official blog
Engineering at Meta
Engineering at Meta
T
Threatpost
Recent Commits to openclaw:main
Recent Commits to openclaw:main
宝玉的分享
宝玉的分享
Security Latest
Security Latest
T
The Exploit Database - CXSecurity.com
博客园_首页
A
Arctic Wolf

博客园 - mabiao008

idea更新代码时Merge和Rebase区别 CC skill Mac下载免费软件遇到问题解决 CQRS 错误码code是int类型好还是String类型好 idea2025版本破解 pgsql切换当前会话的模式 java程序中增加mongodb的联合索引 文档启动脚本报错:-bash: ./app.sh: /bin/bash^M: bad interpreter: No such file or directory Milvus索引 pandoc使用 idea把unicode转为中文 idea 插件分享 java项目处理OFD文件 java项目无法读取resources目录下的文件 三种数据对象的区别 Linux环境aspose插件word转pdf中文乱码解决方案 java List报错Method threw ‘java.lang.UnsupportedOperationException‘ exception. 解决 格式化json大文件 springboot项目启动报错Command line is too long. Shorten the command line via JAR manifest or via a classpath file and rerun. com.alibaba.excel.exception.ExcelGenerateException: Can not close IO linux的TCP端口问题 关于linux端口号 linux下生成pdf文件名遇到问题 把页面查询到的数据导出PDF文件(html中的在线表单下载为pdf文件)
java字段值为null,转json后不存在该字段对应的key
mabiao008 · 2023-03-03 · via 博客园 - mabiao008

在 Java 中,将对象转换为 JSON 字符串通常使用第三方库(如 Jackson、Gson 等)。对于字段值为 null 的情况,通常存在两种处理方式:

  1. 对应的 JSON key 不存在。 这是默认情况,通常情况下这也是比较合理的做法,因为 null 表示缺失或者无值,而在 JSON 中,一个 key 的存在通常表示该值是有意义的,因此将 null 转换为不存在的 key 更符合语义。例如,对于 Java 对象:

    class Person { private String name; private Integer age;

    转换为 JSON 字符串后,如果 name 字段有值而 age 字段为 null,则对应的 JSON 字符串为:

  2. 对应的 JSON key 存在,但值为 null。 如果希望在 JSON 中保留这个 key,而不是将其删除,可以使用一些库的配置参数,比如 Jackson 中可以使用 SerializationFeature.WRITE_NULL_MAP_VALUES 属性。例如,对于上述的 Person 对象,如果希望在 JSON 中保留 age 字段,可以这样配置:

    java

    ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, true); String jsonString = objectMapper.writeValueAsString(person);

    这样,如果 name 字段有值而 age 字段为 null,则对应的 JSON 字符串为:

    json

    {"name":"John","age":null}

    但是java字段值为“”,转json后就会存在该字段对应的key