在使用JSONLib包的应用中,我们实际情况会考虑过滤其某些属性或格式其某些属性。那么如何实现,JSONlib包提供了一个JsonConfig类扩展结构,供开发人员按照实际情况的需要实现Bean2JSONObject的处理过程。
作者写了一个处理Date格式与过滤掉其中一个属性的案例,可以参考。
另外在探讨过程中,作者发现jsonlib不能处理内部类。
package com.china;
import java.util.Date;
public class Teacher implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name ="haibing.xiao";
private int age=27 ;
private Boolean sex =true ;
private Date birthday =new Date();
private Date workDate =new Date();
public Date getWorkDate() {
return workDate;
}
public void setWorkDate(Date workDate) {
this.workDate = workDate;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public Boolean getSex() {
return sex;
}
public void setSex(Boolean sex) {
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.china;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.PropertyFilter;
/**
* @author haibing.xiao
* @since jdk1.6
* <p> className :SimpleJSONlib,Teacher</p>
* */
public class SimpleJSONlib {
public static void main(String[] args) {
JsonConfig jsonConfig =new JsonConfig() ;
/** format classType Date return 2013-01-17*/
jsonConfig.registerJsonValueProcessor(Date.class,new JsonValueProcessor() {
@Override
public Object processObjectValue(String key,Object value,JsonConfig arg2) {
// TODO Auto-generated method stub
return new SimpleDateFormat("yyyy-MM-dd").format(value);
}
@Override
public Object processArrayValue(Object value,JsonConfig arg1) {
// TODO Auto-generated method stub
return value;
}
});
/** filter filedName workDate*/
jsonConfig.setJavaPropertyFilter(new PropertyFilter() {
@Override
public boolean apply(Object source,String name,Object value) {
// TODO Auto-generated method stub
if("workDate".equals(name)){
return true ;
}
return false;
}
});
List<Teacher> list =new ArrayList<Teacher>();
list.add(new Teacher());
list.add(new Teacher());
list.add(new Teacher());
System.out.println(JSONArray.fromObject(list.toArray(),jsonConfig).toString());
}
}
原文链接:https://www.f2er.com/json/290551.html在使用JSONLib包的应用中,我们实际情况会考虑过滤其某些属性或格式其某些属性。那么如何实现,JSONlib包提供了一个JsonConfig类扩展结构,供开发人员按照实际情况的需要实现Bean2JSONObject的处理过程。 作者写了一个处理Date格式与过滤掉其中一个属性的案例,可以参考。
另外在探讨过程中,作者发现jsonlib不能处理内部类。
package com.china; import java.util.Date; public class Teacher implements java.io.Serializable { /** * */ private static final long serialVersionUID = 1L; private String name ="haibing.xiao"; private int age=27 ; private Boolean sex =true ; private Date birthday =new Date(); private Date workDate =new Date(); public Date getWorkDate() { return workDate; } public void setWorkDate(Date workDate) { this.workDate = workDate; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public static long getSerialversionuid() { return serialVersionUID; } public Boolean getSex() { return sex; } public void setSex(Boolean sex) { this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } package com.china; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import net.sf.json.JSONArray; import net.sf.json.JsonConfig; import net.sf.json.processors.JsonValueProcessor; import net.sf.json.util.PropertyFilter; /** * @author haibing.xiao * @since jdk1.6 * <p> className :SimpleJSONlib,Teacher</p> * */ public class SimpleJSONlib { public static void main(String[] args) { JsonConfig jsonConfig =new JsonConfig() ; /** format classType Date return 2013-01-17*/ jsonConfig.registerJsonValueProcessor(Date.class,new JsonValueProcessor() { @Override public Object processObjectValue(String key,Object value,JsonConfig arg2) { // TODO Auto-generated method stub return new SimpleDateFormat("yyyy-MM-dd").format(value); } @Override public Object processArrayValue(Object value,JsonConfig arg1) { // TODO Auto-generated method stub return value; } }); /** filter filedName workDate*/ jsonConfig.setJavaPropertyFilter(new PropertyFilter() { @Override public boolean apply(Object source,String name,Object value) { // TODO Auto-generated method stub if("workDate".equals(name)){ return true ; } return false; } }); List<Teacher> list =new ArrayList<Teacher>(); list.add(new Teacher()); list.add(new Teacher()); list.add(new Teacher()); System.out.println(JSONArray.fromObject(list.toArray(),jsonConfig).toString()); } }