Map turn json format preserves null solution value
Encountered in the development of the map data transfer json format, and then map contains null key-value pairs are not transferred, so record it, the following is a workaround
Use fastJson conversion
import com.alibaba.fastjson.JSON Map<String, Object> map = new HashMap<String, Object>(); String toJSON = JSON.toJSONString(map, SerializerFeature.WRITE_MAP_NULL_FEATURES, SerializerFeature.QuoteFieldNames);
The first is WRITE_MAP_NULL_FEATURES showing: retention map median null key-value pairs,
The second is QuoteFieldNames said: output key whether to use double quotes ( "" ) will be key all wrapped up , the default is true
About SerializerFeature property (more commonly used include)
Name Meaning
QuoteFieldNames output key whether to use double quotation marks , the default is true
QuoteFieldNames output key whether to use double quotation marks , the default is true
WriteMapNullValue whether the output value is null field , the default is false
WriteNullNumberAsZero numeric field if it is null, the output is 0, not null
WriteNullListAsEmpty List field if it is null, the output is [], not null
WriteNullStringAsEmpty character type field if it is null, the output is "", rather than null
WriteNullBooleanAsFalse Boolean field if it is null, the output false, rather than null