使用JSON.toJSONString格式化成json字符串时保留null属性

2022-07-14,,,,

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属性.doc》

下载本文的Word格式文档,以方便收藏与打印。