对象转JSONArray,JSONObject[包括对象中日期格式化,属性过滤]
2016-05-04 15:53
519 查看
创建时间转换器
创建对象属性过滤器,并能对对象中date格式字段进行字符串格式转换
调用方法,
例:将exam对象转换成JSONObject并只保留需要的字段
例:将list<Course>对象转换成JSONArray并只保留Course中需要保留的字段
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);
相关文章推荐
- Windows驱动开发(6) - DRIVER_OBJECT结构体
- String转换成json格式(三):com.alibaba.fastjson.JSONObject
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
- String转换成json格式(二):org.codehaus.jettison.json.JSONObject
- String转换成json格式(一):net.sf.json.JSONObject
- 【Java技术点】java实现map和object互转的三种方法
- Android属性动画(ObjectAnimator、PropertyValuesHolder、ValueAnimator、AnimatorSet)
- placeholder垂直居中(解决光标居中、文字不居中问题)
- Motion-Based Multiple Object Tracking阅读
- The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?...
- Objective-C 高性能的循环
- QMetaObject::connectSlotsByName: No matching signal for问题的解决方法
- json字符串转换为JSONObject和JSONArray
- Runtime方法的使用—Object篇
- Android JsonObject 数据的遍历
- Object C基础学习(三)
- Java Object 的介绍
- juce 中的ReferenceCountedObjectPtr
- 使用Objective-C的文档生成工具:appledoc
- 速战速决 (2) - PHP: 数据类型 bool, int, float, string, object, array