FastJson、Jackson、Gson进行Java对象转换Json的细节处理
转 https://blog.csdn.net/moneyshi/article/details/51830329
Java对象转换Json的细节处理
前言
Java对象在转json的时候,如果对象里面有属性值为null的话,那么在json序列化的时候要不要序列出来呢?对比以下json转换方式
一、fastJson
1、fastJson在转换java对象为json的时候,默认是不序列化null值对应的key的
也就是说当对象里面的属性为空的时候,在转换成json时,不序列化那些为null值的属性
具体案例如下:
AutoPartsSearchRequest 有以下属性:
-
public static void main(String[] args) {
-
AutoPartsSearchRequest request = new AutoPartsSearchRequest();
-
request.setKeywords("123");
-
request.setSortingField("234242");
-
String str = JSONObject.toJSONString(request);//fastjson默认转换是不序列化null值对应的key的
-
System.out.println(str);
-
}
输出结果:{"keywords":"123","sortingField":"234242"} , 没有序列化那些值为null的属性
2、但是如果想把null对应的key序列化出来呢?
那就要仔细看看fastjson转换java对象为json的时候的入参了:也就是这个方法:
JSONObject.toJSONString(Object object, SerializerFeature... features)
Fastjson的SerializerFeature序列化属性:
-
QuoteFieldNames———-输出key时是否使用双引号,默认为true
-
WriteMapNullValue——–是否输出值为null的字段,默认为false
-
WriteNullNumberAsZero—-数值字段如果为null,输出为0,而非null
-
WriteNullListAsEmpty—–List字段如果为null,输出为[],而非null
-
WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,而非null
-
WriteNullBooleanAsFalse–Boolean字段如果为null,输出为false,而非null
-
public static void main(String[] args) {
-
AutoPartsSearchRequest request = new AutoPartsSearchRequest();
-
request.setKeywords("123");
-
request.setSortingField("234242");
-
String str = JSONObject.toJSONString(request, SerializerFeature.WriteMapNullValue);
-
System.out.println(str);
-
}
输出结果如下:
3、想字符类型字段如果为null,转换输出为”“,而非null ,需要多加一个参数:WriteNullStringAsEmpty, 案例如下:
-
public static void main(String[] args) {
-
AutoPartsSearchRequest request = new AutoPartsSearchRequest();
-
request.setKeywords("123");
-
request.setSortingField("234242");
-
String str = JSONObject.toJSONString(request, SerializerFeature.WriteMapNullValue,
-
SerializerFeature.WriteNullStringAsEmpty);
-
System.out.println(str);
-
}
输出结果如下:
二、Jackson
1、jackson默认是序列化null对应的key的,也就是说不管你对象属性有没有值,在转换json的时候都会被序列化出来
-
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
-
AutoPartsSearchRequest request = new AutoPartsSearchRequest();
-
request.setKeywords("123");
-
request.setSortingField("234242");
-
ObjectMapper mapper = new ObjectMapper();
-
String str = mapper.writeValueAsString(request);
-
System.out.println(str);
-
//输出结果(此处就不格式化了):{"sortingField":"234242","partsClassifyId":null,"partsSubClassifyId":null,"sortingDirection":null:......
-
}
2、同理,想要不序列化null也是可以的,具体如下:
-
1.实体上
-
-
-
-
//将该标记放在属性上,如果该属性为NULL则不参与序列化
-
//如果放在类上边,那对这个类的全部属性起作用
-
//Include.Include.ALWAYS 默认
-
//Include.NON_DEFAULT 属性为默认值不序列化
-
//Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化
-
//Include.NON_NULL 属性为NULL 不序列化
-
-
-
2.代码上
-
ObjectMapper mapper = new ObjectMapper();
-
-
mapper.setSerializationInclusion(Include.NON_NULL);
-
-
//通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化
-
//Include.Include.ALWAYS 默认
-
//Include.NON_DEFAULT 属性为默认值不序列化
-
//Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化
-
//Include.NON_NULL 属性为NULL 不序列化
注意:只对VO起作用,Map List不起作用,另外jackson还能过滤掉你设置的属性,具体的就各位自己去研究源码了
三、Gson
1、gson和fastjson一样,默认是不序列化null值对应的key的,具体案例如下:
-
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
-
AutoPartsSearchRequest request = new AutoPartsSearchRequest();
-
request.setKeywords("123");
-
request.setSortingField("234242");
-
Gson g = new GsonBuilder().create();
-
String str = g.toJson(request);
-
System.out.println(str);
-
//输出结果:{"sortingField":"234242","keywords":"123"}
-
}
2、若是想序列化null值对应的key,只需要将以上创建代码改成以下代码就行:
Gson g = new GsonBuilder().serializeNulls().create();
3、若是想转行null为空字符串"",则需要手动处理了
具体参考:gson转换null为空字符串
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义