对象转JSONArray,JSONObject[包括对象中日期格式化,属性过滤]
来源:互联网 发布:uc矩阵子系统划分例题 编辑:程序博客网 时间:2024/05/29 11:48
创建时间转换器
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);
1 0
- 对象转JSONArray,JSONObject[包括对象中日期格式化,属性过滤]
- JSONObject JSONArray 对象使用
- jsonobject 转换对象时过滤属性
- JQuery操作JSONObject/JSONArray对象
- JSONObject和JSONArray遍历数组与对象
- JSONObject和JSONArray对象的创建方法
- JSONObject和JSONArray遍历数组与对象
- java JSONObject JSONArray对象使用小记
- JSONArray和JSONObject对象之间的操作
- JSONObject和JSONArray遍历数组与对象
- js中日期格式字符串转为日期对象和日期进行格式化
- JAVA中日期格式化
- java中日期格式化
- 使用json-lib将JSONObject转Java对象,JSONArray转Java集合
- JSONArray和JSONObject对象解析和取值
- AJAX JSON中日期类型DateTime格式化的序列化自定义对象以及自定义类型参数的问题处理
- jsonArray转jsonObject
- DataGrid中日期格式化方法
- 大端与小端存储模式详解
- jgit pull 分之代码
- ionic js学习笔记
- Java基础知识IO流(其他类)
- 增强Eclipse、MyEclipse的代码自动提示功能
- 对象转JSONArray,JSONObject[包括对象中日期格式化,属性过滤]
- 深入Spring
- javaweb学习总结(三十四)——使用JDBC处理MySQL大数据
- java实现顺序查找、二分查找、哈希表查找、二叉排序树查找
- 逻辑行计数
- android 5.0 zygote启动代码流程
- JavaScript 复选框全选和反选
- windows客户端性能测试之内存泄露检查工具umdh.exe
- boost仅运行一次的例程