FastJson生成json时,显示Null属性

来源:互联网 发布:网络对我们的坏处10条 编辑:程序博客网 时间:2024/06/05 15:28

   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 , 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,SerializerFeature.WriteMapNullValue);System.out.println(str);//输出结果:{"a":1,"b":"","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

0 1
原创粉丝点击