简体   繁体   English

反对Json,仅包含不为null的字段

[英]Object to Json including only fields not null

I have an object named Item with plenty of fields. 我有一个名为Item的对象,其中包含很多字段。 I'm using a Java JSON (json-io) library to serialize its state in a File: 我正在使用Java JSON(json-io)库在文件中序列化其状态:

String json = JsonWriter.objectToJson(item);
// Write String to File

I'd like to make the json String less verbose so that it does not include null values (if possible also boolean which are false). 我想使json字符串更不冗长,以便它不包含空值(如果可能,还返回布尔值,它为false)。 Is it possible to do it somehow ? 有可能以某种方式做到吗?
Thanks 谢谢

I can recommend using jackson mapper if possible. 如果可以的话,我建议使用杰克逊映射器。 Check out these two questions: 查看以下两个问题:

Jackson serialization: ignore empty values (or null) Jackson序列化:忽略空值(或null)

How to tell Jackson to ignore a field during serialization if its value is null? 如果字段为空,如何告诉杰克逊在序列化期间忽略该字段?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM