广告
返回顶部
首页 > 资讯 > 后端开发 > Python >使用JSON.toJSONString格式化成json字符串时保留null属性
  • 708
分享到

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

2024-04-02 19:04:59 708人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录JSON.tojsONString格式化成json字符串时保留null属性属性说明例子处理返回结果中字段为空或为null,不展示字段的问题(字段展示不全)JSON.toJSONS

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;
 

@Configuration
public class FastJsonConfiguration extends WebMvcConfigurationSupport {
 
    
    @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);
    }
 
    
    @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/");
    } 
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: 使用JSON.toJSONString格式化成json字符串时保留null属性

本文链接: https://www.lsjlt.com/news/151860.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作