对象转JSONArray,JSONObject[包括对象中日期格式化,属性过滤]

创建时间转换器

[java]  view plain  copy
  1. import java.text.SimpleDateFormat;  
  2. import java.util.Date;  
  3. import java.util.Locale;  
  4.   
  5. import net.sf.json.JsonConfig;  
  6. import net.sf.json.processors.JsonValueProcessor;  
  7.   
  8.   
  9. public class JsonDateValueProcessor  implements JsonValueProcessor {  
  10.     private String format ="yyyy-MM-dd hh:mm:ss";  
  11.       
  12.     public JsonDateValueProcessor() {  
  13.         super();  
  14.     }  
  15.       
  16.     public JsonDateValueProcessor(String format) {  
  17.         super();  
  18.         this.format = format;  
  19.     }  
  20.   
  21.     public Object processArrayValue(Object paramObject,  
  22.             JsonConfig paramJsonConfig) {  
  23.         return process(paramObject);  
  24.     }  
  25.   
  26.     public Object processObjectValue(String paramString, Object paramObject,  
  27.             JsonConfig paramJsonConfig) {  
  28.         return process(paramObject);  
  29.     }  
  30.       
  31.       
  32.     private Object process(Object value){  
  33.         if(value instanceof Date){    
  34.             SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA);    
  35.             return sdf.format(value);  
  36.         }    
  37.         return value == null ? "" : value.toString();    
  38.     }  
  39.   
  40.   
  41. }  


创建对象属性过滤器,并能对对象中date格式字段进行字符串格式转换


[java]  view plain  copy
  1. import java.lang.reflect.Field;  
  2. import java.util.Date;  
  3.   
  4. import net.sf.json.JSONArray;  
  5. import net.sf.json.JSONObject;  
  6. import net.sf.json.JsonConfig;  
  7. import net.sf.json.util.PropertyFilter;  
  8.   
  9. import org.apache.commons.logging.Log;  
  10. import org.apache.commons.logging.LogFactory;  
  11.     
  12. /** 
  13.  * <p>Title: 保留属性</p> 
  14.  * <p>Description:保留JAVABEAN的指定属性</p> 
  15.  *  
  16.  */  
  17. public class IgnoreFieldProcessorImpl implements PropertyFilter {  
  18.     
  19.     Log log = LogFactory.getLog(this.getClass());  
  20.     
  21.     /** 
  22.      * 保留的属性名称 
  23.      */  
  24.     private String[] fields;  
  25.     
  26.     /** 
  27.      * 空参构造方法<br/> 
  28.      * 默认不忽略集合 
  29.      */  
  30.     public IgnoreFieldProcessorImpl() {  
  31.         // empty  
  32.     }  
  33.     
  34.     /** 
  35.      * 构造方法 
  36.      * @param fields 保留属性名称数组 
  37.      */  
  38.     public IgnoreFieldProcessorImpl(String[] fields) {  
  39.         this.fields = fields;   
  40.     }  
  41.     
  42.     /** 
  43.      * 构造方法 
  44.      * @param fields    保留属性名称数组 
  45.      */  
  46.     public IgnoreFieldProcessorImpl(boolean ignoreColl, String[] fields) {  
  47.         this.fields = fields;  
  48.     }  
  49.     
  50.     public boolean apply(Object source, String name, Object value) {  
  51.         Field declaredField = null;  
  52.           
  53.         // 保留设定的属性  
  54.         if(fields != null && fields.length > 0) {  
  55.             if(juge(fields,name)) {    
  56.                  return false;    
  57.             } else {    
  58.                 return true;   
  59.                  
  60.             }   
  61.         }  
  62.             
  63.         return false;  
  64.     }  
  65.     /** 
  66.      * 保留相等的属性 
  67.      * @param s 
  68.      * @param s2 
  69.      * @return 
  70.      */  
  71.      public boolean juge(String[] s,String s2){    
  72.          boolean b = false;    
  73.          for(String sl : s){    
  74.              if(s2.equals(sl)){    
  75.                  b=true;    
  76.              }    
  77.          }    
  78.          return b;    
  79.      }    
  80.        
  81.      /** 
  82.       * 获取保留的属性 
  83.       * @param fields 
  84.       */  
  85.     public String[] getFields() {  
  86.         return fields;  
  87.     }  
  88.     
  89.     /** 
  90.      * 设置保留的属性 
  91.      * @param fields 
  92.      */  
  93.     public void setFields(String[] fields) {  
  94.         this.fields = fields;  
  95.     }  
  96.       
  97.     /** 
  98.      * 保留字段转换json 对象 
  99.      * @param configs 保留字段名称 
  100.      * @param entity 需要转换实体 
  101.      * @return 
  102.      */  
  103.     public static JSONObject JsonConfig(String[] configs,Object entity){  
  104.         JsonConfig config = new JsonConfig();  
  105.         config.setJsonPropertyFilter(new IgnoreFieldProcessorImpl(true, configs)); // 保留的属性<span style="font-family: Arial, Helvetica, sans-serif;">configs</span>  
  106.         config.registerJsonValueProcessor(Date.classnew JsonDateValueProcessor()); // 将对象中的日期进行格式化  
  107.         JSONObject fromObject = JSONObject.fromObject(entity, config );  
  108.         return fromObject;  
  109.    
  110.     }  
  111.       
  112.       
  113.     /** 
  114.      * 保留字段转换json 数组 
  115.      * @param configs 保留字段名称 
  116.      * @param entity 需要转换实体 
  117.      * @return 
  118.      */  
  119.     public static JSONArray ArrayJsonConfig(String[] configs,Object entity){  
  120.         JsonConfig config = new JsonConfig();  
  121.         config.setJsonPropertyFilter(new IgnoreFieldProcessorImpl(true, configs)); //<span style="font-family: Arial, Helvetica, sans-serif;">保留的属性</span><span style="font-family: Arial, Helvetica, sans-serif;">configs</span>  
  122.         config.registerJsonValueProcessor(Date.classnew JsonDateValueProcessor());  
  123.         JSONArray fromObject = JSONArray.fromObject(entity, config );  
  124.         return fromObject;  
  125.     }  
  126.       
  127. }  

调用方法,

例:将exam对象转换成JSONObject并只保留需要的字段

[java]  view plain  copy
  1. JSONObject ex = IgnoreFieldProcessorImpl.JsonConfig(new String[]{"id","examName","examDate"}, exam);  


例:将list<Course>对象转换成JSONArray并只保留Course中需要保留的字段

[java]  view plain  copy
  1. JSONArray listj=IgnoreFieldProcessorImpl.ArrayJsonConfig(new String[]{"id","courseName","examDate","remark"}, list);  
  2.                   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值