当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 对象转JSONArray,JSONObject的实例教程,包括对象中日期格式化,属性过滤

对象转JSONArray,JSONObject的实例教程,包括对象中日期格式化,属性过滤

2018年10月15日  | 移动技术网IT编程  | 我要评论

创建时间转换器

import java.text.simpledateformat;  
import java.util.date;  
import java.util.locale;  
  
import net.sf.json.jsonconfig;  
import net.sf.json.processors.jsonvalueprocessor;  
  
  
public class jsondatevalueprocessor  implements jsonvalueprocessor {  
    private string format ="yyyy-mm-dd hh:mm:ss";  
      
    public jsondatevalueprocessor() {  
        super();  
    }  
      
    public jsondatevalueprocessor(string format) {  
        super();  
        this.format = format;  
    }  
  
    public object processarrayvalue(object paramobject,  
            jsonconfig paramjsonconfig) {  
        return process(paramobject);  
    }  
  
    public object processobjectvalue(string paramstring, object paramobject,  
            jsonconfig paramjsonconfig) {  
        return process(paramobject);  
    }  
      
      
    private object process(object value){  
        if(value instanceof date){    
            simpledateformat sdf = new simpledateformat(format,locale.china);    
            return sdf.format(value);  
        }    
        return value == null ? "" : value.tostring();    
    }  
  
  
}  

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

import java.lang.reflect.field;  
import java.util.date;  
  
import net.sf.json.jsonarray;  
import net.sf.json.jsonobject;  
import net.sf.json.jsonconfig;  
import net.sf.json.util.propertyfilter;  
  
import org.apache.commons.logging.log;  
import org.apache.commons.logging.logfactory;  
    
/** 
 * <p>title: 保留属性</p> 
 * <p>description:保留javabean的指定属性</p> 
 *  
 */  
public class ignorefieldprocessorimpl implements propertyfilter {  
    
    log log = logfactory.getlog(this.getclass());  
    
    /** 
     * 保留的属性名称 
     */  
    private string[] fields;  
    
    /** 
     * 空参构造方法<br/> 
     * 默认不忽略集合 
     */  
    public ignorefieldprocessorimpl() {  
        // empty  
    }  
    
    /** 
     * 构造方法 
     * @param fields 保留属性名称数组 
     */  
    public ignorefieldprocessorimpl(string[] fields) {  
        this.fields = fields;   
    }  
    
    /** 
     * 构造方法 
     * @param fields    保留属性名称数组 
     */  
    public ignorefieldprocessorimpl(boolean ignorecoll, string[] fields) {  
        this.fields = fields;  
    }  
    
    public boolean apply(object source, string name, object value) {  
        field declaredfield = null;  
          
        // 保留设定的属性  
        if(fields != null && fields.length > 0) {  
            if(juge(fields,name)) {    
                 return false;    
            } else {    
                return true;   
                 
            }   
        }  
            
        return false;  
    }  
    /** 
     * 保留相等的属性 
     * @param s 
     * @param s2 
     * @return 
     */  
     public boolean juge(string[] s,string s2){    
         boolean b = false;    
         for(string sl : s){    
             if(s2.equals(sl)){    
                 b=true;    
             }    
         }    
         return b;    
     }    
       
     /** 
      * 获取保留的属性 
      * @param fields 
      */  
    public string[] getfields() {  
        return fields;  
    }  
    
    /** 
     * 设置保留的属性 
     * @param fields 
     */  
    public void setfields(string[] fields) {  
        this.fields = fields;  
    }  
      
    /** 
     * 保留字段转换json 对象 
     * @param configs 保留字段名称 
     * @param entity 需要转换实体 
     * @return 
     */  
    public static jsonobject jsonconfig(string[] configs,object entity){  
        jsonconfig config = new jsonconfig();  
        config.setjsonpropertyfilter(new ignorefieldprocessorimpl(true, configs)); // 保留的属性<span style="font-family: arial, helvetica, sans-serif;">configs</span>  
        config.registerjsonvalueprocessor(date.class, new jsondatevalueprocessor()); // 将对象中的日期进行格式化  
        jsonobject fromobject = jsonobject.fromobject(entity, config );  
        return fromobject;  
   
    }  
      
      
    /** 
     * 保留字段转换json 数组 
     * @param configs 保留字段名称 
     * @param entity 需要转换实体 
     * @return 
     */  
    public static jsonarray arrayjsonconfig(string[] configs,object entity){  
        jsonconfig config = new jsonconfig();  
        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>  
        config.registerjsonvalueprocessor(date.class, new jsondatevalueprocessor());  
        jsonarray fromobject = jsonarray.fromobject(entity, config );  
        return fromobject;  
    }  
      
}  

调用方法,

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

jsonobject ex = ignorefieldprocessorimpl.jsonconfig(new string[]{"id","examname","examdate"}, exam);  

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

jsonarray listj=ignorefieldprocessorimpl.arrayjsonconfig(new string[]{"id","coursename","examdate","remark"}, list);  

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网