STRUTS2 Fastjson,jackson conversion json simple to use

Source: Internet
Author: User

One. Fastjson use

1. Import Fastjson.jar Package

2. How to use

Use the same method for objects and collections

Returns a JSON object using Json.tojsonstring (object)

3. If you format the output for the date type

@JSONField (format= "YYYY-MM-DD")

Private Date birthday;

4. About whether the property is generated in the JSON string set

In action, new a Serializefilter

 serializefilter filter = new PropertyFilter () {
@Override
Public Boolean apply (Object object, String Nam E, Object value) {
System.out.println (object);//object to be converted to JSON
System.out.println (name);//property names
System.out.println (value);///property value
if ("id". Equals (name)) {
Return false;//returns false to indicate that the JSON string is not generated in the
The
return true;//represents the build in the JSON string
}
};
Json.tojsonstring (object, filter);

Two. Use of Jackson
1. Import Jackson's Jackson.jar package
2. Using method
Use the same method for objects and collections
Objectmapper Mapp ER = new Objectmapper ();
Sring JSON = mapper.writevalueasstring (object);
3. Processing Date type
Objectmapper mapper = new Objectmapper ();
Mapper.setdateformat (New SimpleDateFormat ("Yyyy-mm-dd");//Set Date Formatter
Sring JSON = mapper.writevalueasstring (object);
4. Filtering Properties
1. All attribute filters under the entity class
Add annotations to an entity class
@JsonIgnoreProperties ({"id", "releasedate"})//filter properties are separated by commas
public class product{
private int id;
private Sting name;
Private Date ReleaseDate;
}
2. Attribute filtering for a single action
@JsonFilter ("Productfilter")//Use annotations to define the filtered entity class name
public class product{
private int id;
private Sting name;
Private Date ReleaseDate;
}
Implementation of encoding in action
Filterprovider fp = new Simplefilterprovider (). addfilter ("Productfilter",
Simplebeanpropertyfilter.filteroutallexcept ("id", "name");//contains only ID and name
Filterprovider fp = new Simplefilterprovider (). addfilter ("Productfilter",
Simplebeanpropertyfilter.serializeallexcept ("id", "name");//does not contain ID and name
Mapper.setfilters (FP);
    
String JSON = mapper.writevalueasstring (object);
    


STRUTS2 Fastjson,jackson conversion json simple to use

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.