FastJson生成json时,显示Null属性

前端之家收集整理的这篇文章主要介绍了FastJson生成json时,显示Null属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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


  1. Map < String,Object > jsonMap = new HashMap< String,Object>();
  2. jsonMap.put("a",1);
  3. jsonMap.put("b","");
  4. jsonMap.put("c",null);
  5. jsonMap.put("d","wuzhuti.cn");
  6.  
  7.  
  8. String str = JSONObject.toJSONString(jsonMap);
  9. System.out.println(str);
  10. //输出结果:{"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

  1. Map < String,"wuzhuti.cn");
  2.  
  3. String str = JSONObject.toJSONString(jsonMap,SerializerFeature.WriteMapNullValue);
  4. System.out.println(str);
  5. //输出结果:{"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或者空[]

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



完整的选项

  1. package com.alibaba.fastjson.serializer;
  2.  
  3. /**
  4. * @author wenshao[szujobs@hotmail.com]
  5. */
  6. public enum SerializerFeature {
  7. QuoteFieldNames,/**
  8. *
  9. */
  10. UseSingleQuotes,/**
  11. *
  12. */
  13. WriteMapNullValue,/**
  14. * 用枚举toString()值输出
  15. */
  16. WriteEnumUsingToString,/**
  17. * 用枚举name()输出
  18. */
  19. WriteEnumUsingName,/**
  20. *
  21. */
  22. UseISO8601DateFormat,/**
  23. * @since 1.1
  24. */
  25. WriteNullListAsEmpty,/**
  26. * @since 1.1
  27. */
  28. WriteNullStringAsEmpty,/**
  29. * @since 1.1
  30. */
  31. WriteNullNumberAsZero,/**
  32. * @since 1.1
  33. */
  34. WriteNullBooleanAsFalse,/**
  35. * @since 1.1
  36. */
  37. SkipTransientField,/**
  38. * @since 1.1
  39. */
  40. SortField,/**
  41. * @since 1.1.1
  42. */
  43. @Deprecated
  44. WriteTabAsSpecial,/**
  45. * @since 1.1.2
  46. */
  47. PrettyFormat,/**
  48. * @since 1.1.2
  49. */
  50. WriteClassName,/**
  51. * @since 1.1.6
  52. */
  53. DisableCircularReferenceDetect,/**
  54. * @since 1.1.9
  55. */
  56. WriteSlashAsSpecial,/**
  57. * @since 1.1.10
  58. */
  59. BrowserCompatible,/**
  60. * @since 1.1.14
  61. */
  62. WriteDateUseDateFormat,/**
  63. * @since 1.1.15
  64. */
  65. NotWriteRootClassName,/**
  66. * @since 1.1.19
  67. */
  68. DisableCheckSpecialChar,/**
  69. * @since 1.1.35
  70. */
  71. BeanToArray,/**
  72. * @since 1.1.37
  73. */
  74. WriteNonStringKeyAsString,/**
  75. * @since 1.1.42
  76. */
  77. NotWriteDefaultValue,/**
  78. * @since 1.2.6
  79. */
  80. BrowserSecure,/**
  81. * @since 1.2.7
  82. */
  83. IgnoreNonFieldGetter
  84. ;


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

猜你在找的Json相关文章