本文共 3723 字,大约阅读时间需要 12 分钟。
创建时间转换器
- 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;
-
-
-
-
-
-
- public class IgnoreFieldProcessorImpl implements PropertyFilter {
-
- Log log = LogFactory.getLog(this.getClass());
-
-
-
-
- private String[] fields;
-
-
-
-
-
- public IgnoreFieldProcessorImpl() {
-
- }
-
-
-
-
-
- public IgnoreFieldProcessorImpl(String[] fields) {
- this.fields = 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;
- }
-
-
-
-
-
-
- public boolean juge(String[] s,String s2){
- boolean b = false;
- for(String sl : s){
- if(s2.equals(sl)){
- b=true;
- }
- }
- return b;
- }
-
-
-
-
-
- public String[] getFields() {
- return fields;
- }
-
-
-
-
-
- public void setFields(String[] fields) {
- this.fields = fields;
- }
-
-
-
-
-
-
-
- public static JSONObject JsonConfig(String[] configs,Object entity){
- JsonConfig config = new JsonConfig();
- config.setJsonPropertyFilter(new IgnoreFieldProcessorImpl(true, configs));
- config.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
- JSONObject fromObject = JSONObject.fromObject(entity, config );
- return fromObject;
-
- }
-
-
-
-
-
-
-
-
- public static JSONArray ArrayJsonConfig(String[] configs,Object entity){
- JsonConfig config = new JsonConfig();
- config.setJsonPropertyFilter(new IgnoreFieldProcessorImpl(true, configs));
- 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);
-
转载地址:http://ccpwn.baihongyu.com/