使用fastjson转换,pom配置如下
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.72</version>
</dependency>
样例:
public static void main(String[] args){
Map map = new HashMap();
map.put("test",null);
map.put("temp","temp");
System.out.println(
JSON.toJSONString(map, SerializerFeature.WRITE_MAP_NULL_FEATURES,SerializerFeature.QuoteFieldNames)
);
}
SerializerFeature.WRITE_MAP_NULL_FEATURES:保留map中值为null的键值对,
SerializerFeature.QuoteFieldNames:输出key时是否使用双引号("")将键值都包裹起来,默认为true(使用WRITE_MAP_NULL_FEATURES,不使用QuoteFieldNames,key值则没有""包裹)
扩展: