Although it is very simple to use Fastjson to generate JSON data, I have found some notable things in the actual use process, such as: previously written jquery parsing Fastjson generated JSON data will appear error
Today I'm going to record some other points of attention.
How do I filter out some fields when I convert an object to a JSON string?
Method One:
Annotate the JavaBean object's corresponding field so that the generated JSON does not contain the field
@JSONField (serialize=false) private String userName;
Method Two:
Use the Fastjson filter when you need to generate JSON
PropertyFilter Profilter =NewPropertyFilter () {@Override Public Booleanapply (Object object, String name, Object value) {if(Name.equalsignorecase ("Age")){ //False indicates that the age field will be excluded return false; } return true; } }; JSON=json.tojsonstring (user, profilter); SYSTEM.OUT.PRINTLN (JSON);
I personally recommend the use of the second, although the code is a bit more, but if there is a need for the field to not filter the situation, it is not very flexible
Format Date Time
When the date time in the database is generated JSON, it may occur in milliseconds or the format is inconsistent, how should you specify the date time format to use? Use the Format property in the @jsonfield annotation.
or use annotations in JavaBean date-time fields
// Format a date by using annotations to convert to JSON @JSONField (format= "Yyyy-mm-dd HH:mm:ss") private date date;
Fastjson using Notes