How to convert the Null to an empty string when fastjson generates json
This article focuses on "how to achieve fastjson to generate json Null into an empty string", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to convert the Null to an empty string when fastjson generates json.
For instance
Generate JSON code snippet
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); / / output result: {" a ": 1," b ":", d: "wuzhuti.cn"}
From the output, we can see that the key corresponding to null has been filtered out; this is obviously not what we want, so we need to use the SerializerFeature serialization genus of fastjson.
This is the method: JSONObject.toJSONString (Object object, SerializerFeature...) Features)
Whether Fastjson's SerializerFeature serialization attribute QuoteFieldNames---- uses double quotes when outputting key. Default is whether true WriteMapNullValue--- outputs fields with a value of null. Default is false WriteNullNumberAsZero-- numeric field if null, output is 0, and non-null WriteNullListAsEmpty--List field is null, output is [], and non-null WriteNullStringAsEmpty- character type field is null, output is "", instead of null WriteNullBooleanAsFalse-Boolean field, if null, output is false, not nullMap.
< 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) / / output result: {"a": 1, "b": "," c ": null," d ":" wuzhuti.cn "} so far, I believe you have a deeper understanding of" how to realize how to convert Null to an empty string when fastjson generates json ". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!