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