JSON.toJSONString格式化成json字符串时保留null属性
第一篇 JSON.toJSONString格式化成json字符串时保留null属性
原文:https://blog.csdn.net/qq_34412985/article/details/81985459
使用阿里的
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转成0SerializerFeature.WriteNullNumberAsZero,// 将List类型的null转成[]SerializerFeature.WriteNullListAsEmpty,// 将Boolean类型的null转成falseSerializerFeature.WriteNullBooleanAsFalse,// 避免循环引用SerializerFeature.DisableCircularReferenceDetect);
第二篇 springboot 处理返回结果中字段为空或为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*/@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();FastJsonConfig config = new FastJsonConfig();config.setSerializerFeatures(// 保留map空的字段SerializerFeature.WriteMapNullValue,// 将String类型的null转成""SerializerFeature.WriteNullStringAsEmpty,// 将Number类型的null转成0SerializerFeature.WriteNullNumberAsZero,// 将List类型的null转成[]SerializerFeature.WriteNullListAsEmpty,// 将Boolean类型的null转成falseSerializerFeature.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*/@Overridepublic 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属性相关推荐
- oracle json字符串转数组,json字符串转化成json对象(原生方法)
json字符串.json对象.数组 三者之间的转换 //json字符串.json对象.数组 三者之间的转换 let jsonStr = '[{"id":"01" ...
- JSON的正确格式及JSON与实体类相互转换
json是一种与语言无关的数据交换的格式,使用Json的格式与解析方便的可以表示一个对象信息,json有两种格式:①对象格式:{"key1":obj,"key2" ...
- js将数组转化成json
要使用json来传输数据,必须将所承载的数据转换成json的格式.json的格式如下: JSON建构于两种结构: 1. "名称/值"对的集合.不同的语言中,它被理解为对象 ...
- 从永远到永远-JSON转JSON字符串值为null的处理
JSON转JSON字符串值为null的处理 1.需求 2.处理 3.其他问题 1.需求 应要求,将某上古接口数据存入了redis缓存,原接口返回数据格式是Map<String,Object> ...
- 解决JSON中文乱码以及JSON处理Date格式
项目启动保错(jackson版本问题导致项目启动失败) 出现这个错误是因为jackson依赖和springboot依赖版本有冲突,把jsckson的依赖版本从2.9.8换成 2.11.0即可 9. J ...
- json的格式、存储与发送以及多个Json的处理
1.Json的格式 其实json就是对象.源生的js代码并没有类的概念.对象救就是object.对象有自己的属性,也可以有自己的方法.json是一种轻量级的存储和交换信息的语言.他有自己的格式. 较为 ...
- Python接口自动化响应结果json格式化输出+断言响应结果中指定的属性值+其他
一.Python字典 二.Python元组 三.接口响应值转为字典格式 四.接口响应值以json格式输出并添加到allure报告中 五.断言响应结果中指定的属性值 六.TypeError: strin ...
- JSON解析实战篇:JSON数组中含JSON数组
少年最好的地方就是:嘴里说着要放弃,心里却都憋着一口气. 前言 关于JSON和本文中所用的Fastjson介绍,可以查看我的这两篇博客: JSON简介:什么是JSON -> https://bl ...
- C# 利用Newtonsoft.Json将DataTable进行JSON序列化
1 string JsonString = string.Empty; 2 JsonString = JsonConvert.SerializeObject(vds);//json序列化 3 stri ...
最新文章
- 一个蚂蚁攻城狮曾经的辛酸面试历程!
- idea在Mybatis的xml里面写sql时,表名、字段、报红问题的解决方法
- 「高并发秒杀」linux安装软件有哪几种方式
- TCP和UDP是否可以绑定同一端口进行通信
- Asp.Net生命周期系列二
- .NET Core 3.0 新 JSON API - Utf8JsonWriter
- 前端学习(2647):vue3.0的认识
- 转换汇编到shellcode的过程
- python热成像_盘点Terabee传感器家族:TOF测距、3D相机、热成像
- Ubuntu安装好后,没有网络怎么办?
- springboot整合JDBC+Druid
- 88se9230 linux raid,Gen10磁盘控制器Marvell 88SE9230阵列RAID设置方法教程图文
- android获取手机号ip地址,手机ip查询我的地址定位(网站如何抓取用户手机号和IP地址)...
- nacos 2.0 Scanner SubTypesScanner was not configured
- ipv6环境搭建来测试
- 人工智能界专家:现在我们的机器智商还不及老鼠
- STM32F103 - 系统框架
- IE显示对象不支持此属性或方法 的解决方法
- 阿里云本地SSD型实例i2gne云服务器配置性能详解
- Magento获取当前页面URL地址
热门文章
- 【THOI 2012】 社交网络结构洞
- Context-Transformer 迁移学习 few-shot目标混淆问题
- hibernate的orphanRemoval
- java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()Z
- 无法定位程序输入点 xxxxx 于动态链接库 xxx.dll 上
- 苹果不再需要乔布斯 已完成其三大使命 (转)
- python输出到控制台和文件_python 信息同时输出到控制台与文件的实例讲解
- javascript中setTimeout
- ubuntu 中文输入法
- 【渝粤教育】广东开放大学 宏微观经济学 形成性考核 (24)