Accidentally used to Fastjson conversion json in the foreground with JS parsing actually some fields did not, previously used Gson. Lenovo to is not similar to the Gson default will be null value is not displayed, found the following information is indeed so
Go directly to the code.
ImportJava.util.HashMap;ImportJava.util.Map;ImportCom.alibaba.fastjson.JSONObject;ImportCom.alibaba.fastjson.serializer.SerializerFeature; Public class Test2 { /** * @param args * * Public Static void Main(string[] args) {/ * * Quotefieldnames whether to use double quotation marks ———-output key, by default true writemapnullvalue--– whether to output a field with null value, default to False writenullnumberaszero--numeric field if NULL, output is 0, not null writenulllistasempty-–list field if NULL, output is [], NOT NULL Wri tenullstringasempty-character Type field if NULL, output is "" Instead of NULL Writenullbooleanasfalse–boolean field if NULL, the output is false, not NULL */Map < String, Object > Jsonmap =Newhashmap< String, object> (); Jsonmap.put ("Xyw",1); Jsonmap.put ("123",""); Jsonmap.put ("Xuyw",NULL); Jsonmap.put ("Xywa","CSS"); String str = jsonobject.tojsonstring (JSONMAP); System.out.println (str); String str2 = jsonobject.tojsonstring (Jsonmap,serializerfeature.writemapnullvalue); System.out.println (STR2); }}
Output results
{"123":"","xyw":1,"xywa":"css"}{"123":"","xuyw":null,"xyw":1,"xywa":"css"}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Fastjson Null value Processing