1.过滤属性
java bean转json对象的时候,很多不必要属性的可以过虑掉,有如下两种方式:
第一种:我们可以使用JsonConfig.setExcludes()方法
@Test
public void excludesPropertyTest() {
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes(new String[] { "hobby", "dob" });
Student stud = new Student("Sriram", "Kasireddi", 2, "Singing", new Date(110, 4, 6));
JSONObject jsonObject = JSONObject.fromObject(stud, jsonConfig);
System.out.println(jsonObject.toString(2));
}
运行结果:
{
"age": 2,
"firstName": "Sriram",
"lastName": "Kasireddi"
}
第二种:使用PropertyFilter
@Test
public void excludesPropertyByFilterTest() {
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
@Override
public boolean apply(Object source, String name, Object value) {
// TODO Auto-generated method stub
if ("hobby".equals(name) || "dob".equals(name)) {
return true;
}
return false;
}
});
Student stud = new Student("Sriram", "Kasireddi", 2, "Singing", new Date(110, 4, 6));
JSONObject jsonObject = JSONObject.fromObject(stud, jsonConfig);
System.out.println(jsonObject.toString(2));
}
运行结果:
{
"age": 2,
"firstName": "Sriram",
"lastName": "Kasireddi"
}
以上为我们使用两种方法过滤掉了Student类的hobby和dob属性。
2.bean按照指定格式转换为json格式
我们将Student类中的Date类型的属性,按照yyyy_MM_dd格式转化为json
@Test
public void valueProcessorTest() {
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor("dob", new JsonValueProcessor() {
@Override
public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
Date date = (Date) value;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd");
sdf.format(date);
return sdf.format(date);
}
@Override
public Object processArrayValue(Object value, JsonConfig jsonConfig) {
// TODO Auto-generated method stub
return null;
}
});
Student stud = new Student("Sriram", "Kasireddi", 2, "Singing", new Date(100, 4, 6));
JSONObject jsonObject = JSONObject.fromObject(stud, jsonConfig);
System.out.println(jsonObject.toString(2));
}
运行结果:
{
"age": 2,
"dob": "2000_05_06",
"firstName": "Sriram",
"hobby": "Singing",
"lastName": "Kasireddi"
}
3.动态改变属性名称
将Studen类的firstName属性转为json后变为xxoo
@Test
public void propertyProcessorTest() {
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes(new String[] { "dob" });
jsonConfig.registerJsonPropertyNameProcessor(Student.class, new PropertyNameProcessor() {
@Override
public String processPropertyName(Class beanClass, String name) {
if ("firstName".equals(name)) {
return "xxoo";
}
return name;
}
});
Student stud = new Student("Sriram", "Kasireddi", 2, "Singing", new Date(110, 4, 6));
JSONObject jsonObject = JSONObject.fromObject(stud, jsonConfig);
System.out.println(jsonObject.toString(2));
}
运行结果:
{
"age": 2,
"xxoo": "Sriram",
"hobby": "Singing",
"lastName": "Kasireddi"
}
最后附上Student类
package json712.study_json;
import java.util.Date;
public class Student {
private String firstName;
private String lastName;
private int age;
private String hobby;
private Date dob;
public Student() {
}
public Student(String firstName, String lastName, int age, String hobby, Date dob) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.hobby = hobby;
this.dob = dob;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public String toString() {
return "Student[ " + "firstName = " + firstName + ", lastName = " + lastName + ", age = " + age + ", hobby = "
+ hobby + ", dob = " + dob + " ]";
}
}