Generate JSON code snippets
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"}
From the output results, it can be seen that the key corresponding to null has been filtered out; This is obviously not the result we want, so we need to use the SerializerFeature serialization attribute of fastjson.
This is the method: jsonobject.tojsonstring (objectobject, serializer features … features)
SerializerFeature serialization attribute of Fastjson
QuoteFieldNames———- whether double quotation marks are used when outputting key, the default is true
WriteMapNullValue——- Whether to output fields with null values, the default is false
WriteNullNumberAsZero—- if the numeric field is null, the output is 0 instead of null
If the WriteNullListAsEmpty—-List field is null, the output is [], not null
WriteNullStringAsEmpty—-If the character type field is null, the output is “”instead of null.
Writenullbooleanasefalse-if boolean field is null, the output is false, not 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"}