这篇文章主要介绍了使用JSON.toJSONString格式化成json字符串时保留null属性,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
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/");
}
}以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:使用JSON.toJSONString格式化成json字符串时保留null属性
基础教程推荐
猜你喜欢
- JavaWeb 实现验证码功能(demo) 2024-04-14
- springboot下使用shiro自定义filter的个人经验分享 2024-02-27
- Java编写实现窗体程序显示日历 2023-01-02
- 深入理解约瑟夫环的数学优化方法 2024-03-07
- 运用El表达式截取字符串/获取list的长度实例 2023-08-01
- Java+mysql实现学籍管理系统 2023-03-16
- 是否适合从javabean类更新数据库? 2023-11-04
- Java中EnvironmentAware 接口的作用 2023-01-23
- JSP 动态树的实现 2023-12-17
- 使用Java和WebSocket实现网页聊天室实例代码 2024-02-25
