Jsonobject.tojsonstring (Object object, Serializerfeature ... features)
Serializerfeature Some of the useful enumeration values
Quotefieldnames whether to use double quotes when ———-output key, by default True
writemapnullvalue--– whether to output a field with a null value, false by default
writenullnumberaszero--numeric field if NULL, the output is 0, not null
writenulllistasempty-–list field if NULL, the output is [], NOT NULL
writenullstringasempty-character Type field if NULL, the output is "", NOT NULL
Writenullbooleanasfalse–boolean field if NULL, the output is false, not NULL
The above serializerfeature is mainly for the case of the object serialization conversion (this time to determine the type of the parameter), and in the map, you put into null is NULL, the serialization is not able to determine its original type, so it does not work. To use parameters related to NULL in serializerfeature, you should pass in the object for serialization.
Understanding of the Rules:
Serializerfeature.writemapnullvalue if the output value is NULL, the default is false, which means that null is output instead of ignored (default policy is ignored, so null fields are not seen)
writenullstringasempty-character Type field if NULL, the output is "", NOT NULL
Note that the field is a field that is a field, not a json.put ("key", null), so when it is used, the fields are null and can be converted to an empty string.
If you let all null strings in the output JSON become empty strings, the simplest thing to do is to add a value filter, which avoids the phenomenon that some fields are null and some have null characters.
Sample code
Importjava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.HashSet;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Set;ImportCom.alibaba.fastjson.JSON;ImportCom.alibaba.fastjson.JSONObject;Importcom.alibaba.fastjson.serializer.SerializerFeature;ImportCom.alibaba.fastjson.serializer.ValueFilter; Public classDemo1 { Public classStudent {PrivateString name; Private intAge ; Private BooleanIsmale; PrivateStudent GF; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } Public BooleanIsmale () {returnIsmale; } Public voidSetmale (BooleanIsmale) { This. Ismale =Ismale; } PublicStudent GETGF () {returnGF; } Public voidSETGF (Student gf) { This. GF =GF; } } Private StaticValuefilter filter =NewValuefilter () {@Override Publicobject Process (Object obj, String s, Object v) {if(v = =NULL) return""; returnv; } }; Public Static voidMain (string[] args) {NewDemo1 (). Foo (); NewDemo1 (). bar (); } Private voidfoo () {System.out.println ("foo ()---------------------------"); Jsonobject J1=NewJsonobject (); J1.put ("Name", "Zhangsan"); J1.put ("Age", 13); J1.put ("Ismale",true); J1.put ("GF",NULL); Map<string, object> fav =NewHashmap<string, object>(); Set<String> books =NewHashset<string>(); Books.add (Three); Books.add (Historical records); Fav.put ("History", books); String[] Arts=Newstring[] {}; Fav.put ("Arts", arts); String[] Musics=NewString[] {"Welcome to Beijing", "painting the Heart" }; Fav.put ("Musics", musics); List<String> Sports =NewArraylist<string>(); Fav.put ("Sports", sports); J1.put ("Fav", Fav); List<Student> classmates =NewArraylist<student>(); Classmates.add (NewStudent ()); Student Lisi=NewStudent (); Lisi.setmale (false); Lisi.setage (11); Classmates.add (Lisi); Student Zhangsan=NewStudent (); Zhangsan.setage (13); Zhangsan.setname ("Zhang San"); Zhangsan.setmale (true); ZHANGSAN.SETGF (Lisi); Classmates.add (Zhangsan); J1.put ("Classmates", classmates); String Str=NULL; J1.put ("Str", str); System.out.println (J1.tostring ()); System.out println (json.tojsonstring (J1, Serializerfeature.writemapnullvalue, Serializerfeature.writenullstrin Gasempty)); System.out.println (json.tojsonstring (J1, filter, Serializerfeature.writemapnullvalue, Serializerfeature.writen Ullstringasempty)); System.out.println (Json.tojsonstring (J1, serializerfeature.writenullstringasempty)); System.out.println (json.tojsonstring (J1, filter, serializerfeature.writenullstringasempty)); Map<string, jsonobject> m =NewHashmap<string, jsonobject>(); M.put ("Key", J1); System.out.println (Json.tojsonstring (M, serializerfeature.writenonstringkeyasstring, Serializerfeature.writenu Llstringasempty)); System.out.println (Json.tojsonstring (M, filter, serializerfeature.writenonstringkeyasstring, Serializerfeature. Writenullstringasempty)); } Private voidBar () {System.out.println ("Bar ()---------------------------"); Student Zhangsan=NewStudent (); Zhangsan.setage (13); Zhangsan.setname ("Zhang San"); Zhangsan.setmale (true); Student Lisi=NewStudent (); //lisi.setname ("Lisi");Lisi.setmale (false); Lisi.setage (11); ZHANGSAN.SETGF (Lisi); System.out.println (Json.tojsonstring (Zhangsan, Serializerfeature.writemapnullvalue, Serializerfeature.writenul Lstringasempty)); System.out.println (Json.tojsonstring (Zhangsan, Serializerfeature.writemapnullvalue)); System.out.println (Json.tojsonstring (Zhangsan, serializerfeature.writenullstringasempty)); System.out.println (json.tojsonstring (Zhangsan)); System.out.println (json.tojsonstring (Zhangsan, filter)); System.out.println (json.tojsonstring (Zhangsan, filter, Serializerfeature.writemapnullvalue, SERIALIZERFEATURE.W Ritenullstringasempty)); }}
Foo ()---------------------------{"Ismale": True, "name": "Zhangsan", "classmates": [{"Age": 0, "male": false},{"Age" : One, "male": false},{"Age": +, "GF": {"$ref": "$.classmates[1"}, "male": True, "name": "Zhang San"}], "fav": {"Sports": [], " Musics ": [Welcome to Beijing]," painting The Heart "," History ": [" Historical records "," Three Kingdoms "]," arts ": []}," Age ": 13}{" str ": null," Ismale ": True," name ":" Zhangsan "," Classmates ": [{" Age ": 0," GF ": null," male ": false," name ":" ""},{"age": One, "GF": null, "male": false, "name": "},{" age ": 13, "GF": {"$ref": "$.classmates[1]"}, "male": True, "name": "Zhang San"}], "fav": {"Sports": [], "musics": ["Welcome to Beijing", "Painting the Heart"], "history" : ["Historical records", "Three Kingdoms"], "arts": []}, "age": +, "GF": null}{"str": "", "Ismale": True, "name": "Zhangsan", "classmates": [{"Age": 0, " GF ":", "male": false, "name": ""},{"age": One, "GF": "", "male": false, "name": "},{" Age: "" GF ": {" $ref ":" $.classmates[ 1] "}," male ": True," name ":" Zhang San "}]," fav ": {" Sports ": []," musics ": [" Welcome to Beijing "," Painting the Heart "]," history ": [" Historical records "," Three Kingdoms "]," arts ": []}," Age ": +," GF ":" "} {" Ismale ": True," name ":" Zhangsan "," classmates ": [{" Age ": 0," male ": false},{" Age ": One," male ": false}, {"Age": "GF": {"$rEF ":" $.classmates[1] "}," male ": True," name ":" Zhang San "}]," fav ": {" Sports ": []," musics ": [" Welcome to Beijing "," Painting the Heart "]," history ": [" Historical records "," Three Kingdoms "]," arts ": []}," Age ": 13}{" str ":" "," Ismale ": True," name ":" Zhangsan "," classmates ": [{" Age ": 0," GF ":" "," male ": false , "name": ""},{"age": One, "GF": "", "male": false, "name": "},{" Age ":" GF ": {" $ref ":" $.classmates[1] "}," male ": true," Name ":" Zhang San "}]," fav ": {" Sports ": []," musics ": [" Welcome to Beijing "," Painting the Heart "]," history ": [" Historical records "," Three Kingdoms "]," arts ": []}," age ": +," GF ":"} {" Key ": {" Ismale ": True," name ":" Zhangsan "," classmates ": [{" Age ": 0," male ": false},{" Age ": One," male ": false},{" Age ": 13," GF ": {" $ref ":" $.key.classmates[1] "}," male ": True," name ":" Zhang San "}]," fav ": {" Sports ": []," musics ": [" Welcome to Beijing "," Painting the Heart "]," History ": [" Historical records "," Three Kingdoms "]," arts ": []}," Age ": 13}}{" key ": {" str ":", "Ismale": True, "name": "Zhangsan", "classmates": [{' Age ": 0," GF ":" "," male ": false," name ":"},{"age": One, "GF": "", "male": false, "name": "},{" Age ":", "GF": {"$ref": "$." Key.classmates[1] "}," male ": True," name ":" Zhang San "}]," fav ": {" Sports ": []," musics ": [" Welcome to Beijing "," Painting the Heart "]," history ": [" Historical records "," Three Kingdoms "] , "arts": []}, "age": +, "GF": "}}bar ()---------------------------{" Age ": +," GF ": {" age ": One," GF ": null," male ": false," name ":" "}," male ": true," Name ":" {"Zhang San" {"Age": +, "GF": {"age": One, "GF": null, "male": false, "name": null}, "male": True, "name": "Zhang San"} {"Age": "GF" : {"Age": One, "male": false}, "male": True, "name": "Zhang San"} {"Age": All, "GF": {"age": One, "male": false}, "male": True, "name": "Zhang San "} {" Age ": 11," GF ": {" age ": One," GF ":" "," male ": false," name ":" "}," male ": True," name ":" Zhang San "} {" Age ": +" GF ": {" Age ":," GF ":", "male": false, "name": ""}, "male": True, "name": "Zhang San"}
Fastjson output Null is an empty string