jsonArray 和 JavaBean互换

0 篇文章 0 订阅
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; 
} 

} 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值