Ignore fields and format date usages for Fastjson

Source: Internet
Author: User

1. Specify serialization order

The default Fastjson serializes a Java bean, which is serialized according to the alphabetical order of the FieldName, and you can specify the order of the fields by ordinal. This feature requires 1.1.42 or more versions.

1  Public Static classVO {2@JSONField (ordinal = 3)3     Private intF0;4 5@JSONField (ordinal = 2)6     Private intF1;7 8@JSONField (ordinal = 1)9     Private intF2;Ten}

2. Fastjson Ignore field:

1  Public voidTEST01 (){2list<user> list =NewArraylist<>();3 4User user1 =NewUser ("Lisi", 14);5User user2 =NewUser ("Zs", 18);6 7 List.add (user1);8 List.add (user2);9 TenSystem.out.println ("Before field is ignored:" +jsonarray.tojson (list));//before: [{' Name ': ' Lisi ', ' age ': ' 14},{' name ': ' Zs ', ' ['] ': ' One  ASimplepropertyprefilter filter =NewSimplepropertyprefilter (); -Filter.getexcludes (). Add ("Age"); -System.out.println ("After ignoring field:" +jsonobject.tojsonstring (list, filter));//after ignoring: [{"Name": "Lisi"},{"name": "ZS"}] the}

3. Fastjson Formatted Date:

1   Public voidTEST01 () {2 3User Lisi =NewUser ("Lisi", 13,NewDate ());4System.out.println (Lisi);//user{name= ' Lisi ', age=13, Date=fri Mar-22:00:48 CST 2018}5System.out.println (Json.tojsonstringwithdateformat (Lisi, "Yyyy-mm-dd HH:mm:ss. SSS "));6         //{"Age": +, "date": "2018-03-30 21:59:33.204", "name": "Lisi"}7 8}

4. Extract the Ignore field form tool:

1     /**2      * @paramList3      * @paramargs exclude field4      * @return5      */6      Public StaticString List2json (List list,string[] args) {7         if(Args! =NULL&& args.length>0){8Simplepropertyprefilter filter =NewSimplepropertyprefilter ();9              for(String Arg:args) {Ten filter.getexcludes (). Add (arg); One             } A             returnjsonobject.tojsonstring (list, filter); -         } -         returnjsonobject.tojsonstring (list); the}

Ignore fields and format date usages for Fastjson

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.