jsonConfig 过滤掉值为null的属性
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setJsonPropertyFilter( new PropertyFilter(){
public boolean apply(Object source/* 属性的拥有者 */ , String name /*属性名字*/ , Object value/* 属性值 */ ){
// return true to skip name
return value == null;
}
List<JSONObject> jsonLst = new ArrayList<JSONObject>();
//转成json列表
jsonLst = JSONArray.fromObject(resultLst,config);
jsonlib 把java.util.Date 转换成指定的日期格式
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class, new JsonValueProcessor(){
@Override
public Object processArrayValue(Object arg0, JsonConfig arg1) {
return null;
}
//参数1 :属性名;参数2:json对象的值;参数3:jsonConfig对象
@Override
public Object processObjectValue(String key, Object value,JsonConfig config) {
if(value!=null){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = (Date) value;
return sdf.format(date);
}
return null;
}
});
JSONObject.fromObject(javaBean,jsonConfig);
这里使用内部类,也可以定义一个工具类DateJsonValueProcessor implements JsonValueProcessor来实现
<pre name="code" class="java"><pre name="code" class="java">public class JsonDateValueProcessor implements JsonValueProcessor{
private String format ="yyyy-MM-dd HH:mm:ss";
public Object processArrayValue(Object value, JsonConfig arg1) {
return process(value);
}
public Object processObjectValue(String key, Object value, JsonConfig arg2) {
return process(value);
}
private Object process(Object value){
if(value instanceof Date){
SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.UK);
return sdf.format(value);
}
return value == null ? "" : value.toString();
}
}