FastJson生成json时,显示Null属性

FastJson生成json时,认不会输出null字段。

移动端,有时候,需要后端提供完整的字段说明。


Map < String,Object > jsonMap = new HashMap< String,Object>();
jsonMap.put("a",1);
jsonMap.put("b","");
jsonMap.put("c",null);
jsonMap.put("d","wuzhuti.cn");


String str = JSONObject.toJSONString(jsonMap);
System.out.println(str);
//输出结果:{"a":1,"b":"",d:"wuzhuti.cn"}

输出结果可以看出,null对应的key已经被过滤掉;这明显不是我们想要的结果,这时我们就需要用到fastjson的SerializerFeature序列化属性

也就是这个方法JSONObject.toJSONString(Object object,SerializerFeature... features)

Fastjson的SerializerFeature序列化属性

--来自oschina bfleeee博客

QuoteFieldNames———-输出key时是否使用双引号,认为true
WriteMapNullValue——–是否输出值为null的字段,认为false
WriteNullNumberAsZero—-数值字段如果为null,输出为0,而非null
WriteNullListAsEmpty—–List字段如果为null,输出为[],而非null
WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,而非null
WriteNullBooleanAsFalse–Boolean字段如果为null,输出为false,而非null

Map < String,"wuzhuti.cn");

String str = JSONObject.toJSONString(jsonMap,SerializerFeature.WriteMapNullValue);
System.out.println(str);
//输出结果:{"a":1,"c":null,"d":"wuzhuti.cn"}


Spring配置FastJson

<mvc:annotation-driven>
<mvc:message-converters>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<property name="features">
<list>
<value>disableCircularReferenceDetect </value>
<value>WriteMapNullValue</value>
<value>WriteNullListAsEmpty</value>
<value>WriteNullStringAsEmpty</value>
<value>WriteNullNumberAsZero</value>
<value>WriteNullBooleanAsFalse</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>


//模型

public class TopicVo implements Serializable {


private static final long serialVersionUID = 1L;


private Long id;

private String name;


private String nullName;
private Integer intName;
private Long longName;
private Double doubleName;
private Map mapName;
private List listName;
private Map mapNameWithValue = Maps.newHashMap();
private Map mapNameWithValue2 = Maps.newHashMap();
{
mapNameWithValue2.put("a","a");
}
}


//json数据,空的转换成了null或者0或者空[]

{
      "doubleName": 0,"id": 17,"intName": 0,"listName": [],"longName": 0,"mapName": null,"mapNameWithValue": {},"mapNameWithValue2": {
        "a": "a"
      },"name": "好像的","nullName": ""
    }



完整的选项

package com.alibaba.fastjson.serializer;

/**
 * @author wenshao[szujobs@hotmail.com]
 */
public enum SerializerFeature {
    QuoteFieldNames,/**
     * 
     */
    UseSingleQuotes,/**
     * 
     */
    WriteMapNullValue,/**
     * 用枚举toString()值输出
     */
    WriteEnumUsingToString,/**
     * 用枚举name()输出
     */
    WriteEnumUsingName,/**
     * 
     */
    UseISO8601DateFormat,/**
     * @since 1.1
     */
    WriteNullListAsEmpty,/**
     * @since 1.1
     */
    WriteNullStringAsEmpty,/**
     * @since 1.1
     */
    WriteNullNumberAsZero,/**
     * @since 1.1
     */
    WriteNullBooleanAsFalse,/**
     * @since 1.1
     */
    SkipTransientField,/**
     * @since 1.1
     */
    SortField,/**
     * @since 1.1.1
     */
    @Deprecated
    WriteTabAsspecial,/**
     * @since 1.1.2
     */
    PrettyFormat,/**
     * @since 1.1.2
     */
    WriteClassName,/**
     * @since 1.1.6
     */
    disableCircularReferenceDetect,/**
     * @since 1.1.9
     */
    WriteSlashAsspecial,/**
     * @since 1.1.10
     */
    browserCompatible,/**
     * @since 1.1.14
     */
    WriteDateUseDateFormat,/**
     * @since 1.1.15
     */
    NotWriteRootClassName,/**
     * @since 1.1.19
     */
    disableCheckSpecialChar,/**
     * @since 1.1.35
     */
    BeanToArray,/**
     * @since 1.1.37
     */
    WriteNonStringKeyAsstring,/**
     * @since 1.1.42
     */
    NotWriteDefaultValue,/**
     * @since 1.2.6
     */
    browserSecure,/**
     * @since 1.2.7
     */
    IgnoreNonFieldGetter
    ;


参考资料:https://wuzhuti.cn/2175.html

相关文章

AJAX是一种基于JavaScript和XML的技术,能够使网页实现异步交...
在网页开发中,我们常常需要通过Ajax从后端获取数据并在页面...
在前端开发中,经常需要循环JSON对象数组进行数据操作。使用...
AJAX(Asynchronous JavaScript and XML)是一种用于创建 We...
AJAX技术被广泛应用于现代Web开发,它可以在无需重新加载页面...
Ajax是一种通过JavaScript和HTTP请求交互的技术,可以实现无...