jsonArray 和 JavaBean互换
来源:互联网 发布:虚拟机安装mac卡死 编辑:程序博客网 时间:2024/05/04 12:33
import java.lang.reflect.Array;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;@SuppressWarnings("all")public class JsonToJavaUtil {/*** 将json转成成javaBean对象* @param <T> 返回类型* @param json 字符串* @param clazz 需要转换成的类* @return*/public static <T> T[] jsonToJavaBean(String json,Class<T> clazz) { if(json==null || !json.startsWith("[") || !json.endsWith("]") ){throw new RuntimeException("JSONArray 必须以'['开头,以']'结尾");}JSONArray array = JSONArray.fromObject(json); //先读取字符串数组 Object[] objs = array.toArray(); //转成对像数组 if(objs.length>0){ JsonConfig config = new JsonConfig(); config.registerJsonValueProcessor(Date.class,new JsonDateValueProcessor()); T[] tArrs = (T[]) Array.newInstance(clazz,objs.length);for(int i=0;i<objs.length;i++){JSONObject jsonObj = JSONObject.fromObject(objs[i],config); //再使用JsonObject遍历一个个的对像T t = (T) jsonObj.toBean(jsonObj,clazz); //指定转换的类型,但仍需要强制转化-成功tArrs[i]=t;}return tArrs;}return null;}/*** 将javaBean转成json串* @param obj 对象* @param ignore 过滤掉的属性* @return*/public static String javaBeanToJson(Object obj,final String[] ignore){JsonConfig config = new JsonConfig(); config.registerJsonValueProcessor(Date.class,new JsonDateValueProcessor()); PropertyFilter filter = new PropertyFilter(){ public boolean apply(Object source, String name,Object value){ if(ignore!=null && ignore.length>0){for(String s : ignore){if(s!=null && s.equals(name)) { return true; } }}return false; } }; config.setJsonPropertyFilter(filter); JSONArray s = JSONArray.fromObject(obj,config); return s.toString();}}package com.cjonline.foundation.util;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 { /** * datePattern */ private String datePattern = "yyyy-MM-dd"; /** * JsonDateValueProcessor */ public JsonDateValueProcessor() { super(); } public JsonDateValueProcessor(String format) { super(); this.datePattern = format; } public Object processArrayValue(Object value, JsonConfig jsonConfig) { return process(value); } public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) { return process(value); } private Object process(Object value) { try { if (value instanceof Date) { SimpleDateFormat sdf = new SimpleDateFormat(datePattern, Locale.UK); return sdf.format((Date) value); } return value == null ? "" : value.toString(); } catch (Exception e) { return ""; } } public String getDatePattern() { return datePattern; } public void setDatePattern(String pDatePattern){ datePattern = pDatePattern; } }
- jsonArray 和 JavaBean互换
- 通过JSONArray让json和javabean互相转换
- xml 与 javaBean 互换
- jsonArray,JavaBean,List,Map转成json格式
- JSONObject和JSONArray
- JSONObject和JSONArray
- JSONObject和JSONArray
- JSONArray和JSONObject
- JSONArray和JSONObject
- 关于JSONObject和JSONArray
- 使用JSONObject和JSONArray
- JSONObject 和 JSONArray
- JSONObject 和 JSONArray
- JSONObject和JSONArray
- JSONObject和JSONArray
- JSONObject和JSONArray使用
- JSONObject,JSON,和JSONArray
- jsonObject和jsonArray
- 数据仓库基本概念-事实表的三种分类
- This function has none of DETERMINISTIC, NO SQL解决办法
- Xcode调试技巧
- ASP.NET中获取URL重写前的原始地址详解
- 将拥有相同字段的数据项合并
- jsonArray 和 JavaBean互换
- 算法总结:判断一个数是否为素数
- 遥控器待机键的添加
- @RequestMapping 之 @ModelAttribute <不懂 待补>
- ubuntu查看并杀死进程
- iOS的查询、过滤(NSPredicate)
- windows / linux 下修改MYSQL 的root权限或者密码
- 移动版Chrome的开发者调试功能
- 节日促销礼品选择的七大注意事项