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