Fastjson Null value Processing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.