使用JSON.toJSONString格式化成json字符串时保留null属性
作者:qq_34412985 时间:2023-05-10 14:56:34
JSON.toJSONString格式化成json字符串时保留null属性
使用阿里的
com.alibaba.fastjson.JSON
格式化时,默认null属性会被过滤掉,可以设置不过滤null
public static String parseScriptJsonStringWithNullValue(Object obj) {
if (obj == null || (obj instanceof Undefined)) {
return null;
}
return JSON.toJSONString(obj, new SerializeFilter[]{scriptArrayFilter}, SerializerFeature.WriteMapNullValue);
}
指定这个参数即可
SerializerFeature.WriteMapNullValue
属性说明
QuoteFieldNames
———输出key时是否使用双引号,默认为true
WriteMapNullValue
———是否输出值为null的字段,默认为false
WriteNullNumberAsZero
———数值字段如果为null,输出为0,而非null
WriteNullListAsEmpty
———List字段如果为null,输出为[],而非null
WriteNullStringAsEmpty
———字符类型字段如果为null,输出为”“,而非null
WriteNullBooleanAsFalse
———Boolean字段如果为null,输出为false,而非null
例子
String ret = JSON.toJSONStringWithDateFormat(returnValue, "yyyy-MM-dd HH:mm:ss",
SerializerFeature.PrettyFormat,
// 保留map空的字段
SerializerFeature.WriteMapNullValue,
// 将String类型的null转成""
SerializerFeature.WriteNullStringAsEmpty,
// 将Number类型的null转成0
SerializerFeature.WriteNullNumberAsZero,
// 将List类型的null转成[]
SerializerFeature.WriteNullListAsEmpty,
// 将Boolean类型的null转成false
SerializerFeature.WriteNullBooleanAsFalse,
// 避免循环引用
SerializerFeature.DisableCircularReferenceDetect
);
处理返回结果中字段为空或为null,不展示字段的问题(字段展示不全)
package com.aiqin.mgs.market.api.config;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
/**
* description: fastjson处理返回的参数为null、或者不返回
* date: 2019/11/22 15:03
* author: hantao
* version: 1.0
* springboot 处理返回结果中字段为空或为null,不展示字段的问题(字段展示不全)
*/
@Configuration
public class FastJsonConfiguration extends WebMvcConfigurationSupport {
/**
* 使用阿里 fastjson 作为JSON MessageConverter
* @param converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(
// 保留map空的字段
SerializerFeature.WriteMapNullValue,
// 将String类型的null转成""
SerializerFeature.WriteNullStringAsEmpty,
// 将Number类型的null转成0
SerializerFeature.WriteNullNumberAsZero,
// 将List类型的null转成[]
SerializerFeature.WriteNullListAsEmpty,
// 将Boolean类型的null转成false
SerializerFeature.WriteNullBooleanAsFalse,
// 避免循环引用
SerializerFeature.DisableCircularReferenceDetect);
converter.setFastJsonConfig(config);
converter.setDefaultCharset(Charset.forName("UTF-8"));
List<MediaType> mediaTypeList = new ArrayList<>();
// 解决中文乱码问题,相当于在Controller上的@RequestMapping中加了个属性produces = "application/json"
mediaTypeList.add(MediaType.APPLICATION_JSON);
converter.setSupportedMediaTypes(mediaTypeList);
converters.add(converter);
}
/**
* 整合了swagger需要配置swagger拦截
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html","index.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/META-INF/resources/static/");
}
}
来源:https://blog.csdn.net/qq_34412985/article/details/81985459
标签:JSON.toJSONString,格式化,json,null
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java 深入探究讲解简单工厂模式
2022-12-25 12:02:06
![](https://img.aspxhome.com/file/2023/7/83107_0s.png)
详解Spring cloud使用Ribbon进行Restful请求
2021-07-09 11:05:28
Unity调用C++ dll实现打开双目相机
2022-05-28 13:19:51
![](https://img.aspxhome.com/file/2023/0/77270_0s.png)
java发送kafka事务消息的实现方法
2022-05-17 01:18:35
![](https://img.aspxhome.com/file/2023/4/75514_0s.png)
springcloud gateway如何实现路由和负载均衡
2023-01-09 07:13:35
![](https://img.aspxhome.com/file/2023/9/81239_0s.png)
java使用websocket,并且获取HttpSession 源码分析(推荐)
2023-08-04 17:38:05
java Lambda表达式的使用心得
2023-08-18 05:59:51
![](https://img.aspxhome.com/file/2023/8/113498_0s.png)
详解SpringBoot 快速整合MyBatis(去XML化)
2022-08-19 16:42:54
java实现单机版五子棋
2022-12-20 20:15:07
![](https://img.aspxhome.com/file/2023/9/63379_0s.jpg)
Unity游戏开发实现场景切换示例
2022-12-12 16:06:20
![](https://img.aspxhome.com/file/2023/2/113772_0s.png)
Springboot jar主清单属性丢失解决方案
2022-04-06 05:30:26
![](https://img.aspxhome.com/file/2023/4/61124_0s.png)
Java客户端利用Jedis操作redis缓存示例代码
2021-05-31 03:43:42
Java 实现协程的方法
2022-02-18 22:55:05
SpringMVC使用@PathVariable接收参数过程解析
2021-09-03 20:52:41
Java递归基础与递归的宏观语意实例分析
2021-06-24 09:33:10
![](https://img.aspxhome.com/file/2023/7/116227_0s.png)
详解Spring Security认证流程
2022-04-16 15:48:46
![](https://img.aspxhome.com/file/2023/2/83502_0s.png)
c#进程之间对象传递方法
2022-04-22 09:41:10
![](https://img.aspxhome.com/file/2023/0/96340_0s.gif)
java环境变量path和classpath的配置
2023-08-31 01:09:04
![](https://img.aspxhome.com/file/2023/5/58205_0s.jpg)
详解关于java文件下载文件名乱码问题解决方案
2021-07-05 15:27:04
![](https://img.aspxhome.com/file/2023/4/106144_0s.png)
将应用程序进行Spring6迁移的最佳使用方式
2021-08-28 12:03:58
![](https://img.aspxhome.com/file/2023/2/84212_0s.png)