Fastjson using Notes

Source: Internet
Author: User

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

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.