Java操作Json的工具类
package com.baiyyy.polabs.util.json;
importjava.text.ParseException;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Map;
importnet.sf.json.JSONArray;
importnet.sf.json.JSONObject;
importnet.sf.json.JsonConfig;
importnet.sf.json.util.CycleDetectionStrategy;
/**
*处理json的工具类,负责json数据转换成java对象和java对象转换成json
*
*@authoryongtree
*@date2008-11-22上午10:47:13
*@version1.0
*/
publicclassJsonUtil{
/**
*从一个JSON对象字符格式中得到一个java对象
*
*@paramjsonString
*@parampojoCalss
*@return
*/
publicstaticObjectgetObject4JsonString(StringjsonString,ClasspojoCalss){
Objectpojo;
JSONObjectjsonObject=JSONObject.fromObject(jsonString);
pojo=JSONObject.toBean(jsonObject,pojoCalss);
returnpojo;
}
/**
*从jsonHASH表达式中获取一个map,改map支持嵌套功能
*
*@paramjsonString
*@return
*/
publicstaticMapgetMap4Json(StringjsonString){
JSONObjectjsonObject=JSONObject.fromObject(jsonString);
IteratorkeyIter=jsonObject.keys();
Stringkey;
Objectvalue;
MapvalueMap=newHashMap();
while(keyIter.hasNext()){
key=(String)keyIter.next();
value=jsonObject.get(key);
valueMap.put(key,value);
}
returnvalueMap;
}
/**
*从json数组中得到相应java数组
*
*@paramjsonString
*@return
*/
publicstaticObject[]getObjectArray4Json(StringjsonString){
JSONArrayjsonArray=JSONArray.fromObject(jsonString);
returnjsonArray.toArray();
}
/**
*从json对象集合表达式中得到一个java对象列表
*
*@paramjsonString
*@parampojoClass
*@return
*/
publicstaticListgetList4Json(StringjsonString,ClasspojoClass){
JSONArrayjsonArray=JSONArray.fromObject(jsonString);
JSONObjectjsonObject;
ObjectpojoValue;
Listlist=newArrayList();
for(inti=0;i<jsonArray.size();i++){
jsonObject=jsonArray.getJSONObject(i);
pojoValue=JSONObject.toBean(jsonObject,pojoClass);
list.add(pojoValue);
}
returnlist;
}
/**
*从json数组中解析出java字符串数组
*
*@paramjsonString
*@return
*/
publicstaticString[]getStringArray4Json(StringjsonString){
JSONArrayjsonArray=JSONArray.fromObject(jsonString);
String[]stringArray=newString[jsonArray.size()];
for(inti=0;i<jsonArray.size();i++){
stringArray[i]=jsonArray.getString(i);
}
returnstringArray;
}
/**
*从json数组中解析出javaLong型对象数组
*
*@paramjsonString
*@return
*/
publicstaticLong[]getLongArray4Json(StringjsonString){
JSONArrayjsonArray=JSONArray.fromObject(jsonString);
Long[]longArray=newLong[jsonArray.size()];
for(inti=0;i<jsonArray.size();i++){
longArray[i]=jsonArray.getLong(i);
}
returnlongArray;
}
/**
*从json数组中解析出javaInteger型对象数组
*
*@paramjsonString
*@return
*/
publicstaticInteger[]getIntegerArray4Json(StringjsonString){
JSONArrayjsonArray=JSONArray.fromObject(jsonString);
Integer[]integerArray=newInteger[jsonArray.size()];
for(inti=0;i<jsonArray.size();i++){
integerArray[i]=jsonArray.getInt(i);
}
returnintegerArray;
}
/**
*从json数组中解析出javaDate型对象数组,使用本方法必须保证
*
*@paramjsonString
*@return
*@throwsParseException
*/
publicstaticDate[]getDateArray4Json(StringjsonString,StringDataFormat)
throwsParseException{
JSONArrayjsonArray=JSONArray.fromObject(jsonString);
Date[]dateArray=newDate[jsonArray.size()];
StringdateString;
Datedate;
for(inti=0;i<jsonArray.size();i++){
dateString=jsonArray.getString(i);
date=DateUtil.parseDate(dateString,DataFormat);
dateArray[i]=date;
}
returndateArray;
}
/**
*从json数组中解析出javaInteger型对象数组
*
*@paramjsonString
*@return
*/
publicstaticDouble[]getDoubleArray4Json(StringjsonString){
JSONArrayjsonArray=JSONArray.fromObject(jsonString);
Double[]doubleArray=newDouble[jsonArray.size()];
for(inti=0;i<jsonArray.size();i++){
doubleArray[i]=jsonArray.getDouble(i);
}
returndoubleArray;
}
/**
*将java对象转换成json字符串
*
*@paramjavaObj
*@return
*/
publicstaticStringgetJsonString4JavaPOJO(ObjectjavaObj){
JSONObjectjson;
json=JSONObject.fromObject(javaObj);
returnjson.toString();
}
/**
*将java对象转换成json字符串,并设定日期格式
*
*@paramjavaObj
*@paramdataFormat
*@return
*/
publicstaticStringgetJsonString4JavaPOJO(ObjectjavaObj,
StringdataFormat){
JSONObjectjson;
JsonConfigjsonConfig=configJson(dataFormat);
json=JSONObject.fromObject(javaObj,jsonConfig);
returnjson.toString();
}
/**
*JSON时间解析器具
*
*@paramdatePattern
*@return
*/
publicstaticJsonConfigconfigJson(StringdatePattern){
JsonConfigjsonConfig=newJsonConfig();
jsonConfig.setExcludes(newString[]{""});
jsonConfig.setIgnoreDefaultExcludes(false);
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
jsonConfig.registerJsonValueProcessor(Date.class,
newJsonDateValueProcessor(datePattern));
returnjsonConfig;
}
/**
*除去不想生成的字段(特别适合去掉级联的对象)+时间转换
*@paramexcludes除去不想生成的字段
*@paramdatePattern
*@return
*/
publicstaticJsonConfigconfigJson(String[]excludes,StringdatePattern){
JsonConfigjsonConfig=newJsonConfig();
jsonConfig.setExcludes(excludes);
jsonConfig.setIgnoreDefaultExcludes(true);
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
jsonConfig.registerJsonValueProcessor(Date.class,
newJsonDateValueProcessor(datePattern));
returnjsonConfig;
}
}
*******************************************************************************************
日期转换
*******************************************************************************************
packagecom.baiyyy.polabs.util.json;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importnet.sf.json.JsonConfig;
importnet.sf.json.processors.JsonValueProcessor;
/**
*
*@authoryongtree
*@date2008-11-22上午10:54:19
*@version1.0
*/
publicclassJsonDateValueProcessorimplementsJsonValueProcessor{
privateStringformat="yyyy-MM-ddHH:mm:ss";
publicJsonDateValueProcessor(){
}
publicJsonDateValueProcessor(Stringformat){
this.format=format;
}
publicObjectprocessArrayValue(Objectvalue,JsonConfigjsonConfig){
String[]obj={};
if(valueinstanceofDate[]){
SimpleDateFormatsf=newSimpleDateFormat(format);
Date[]dates=(Date[])value;
obj=newString[dates.length];
for(inti=0;i<dates.length;i++){
obj[i]=sf.format(dates[i]);
}
}
returnobj;
}[color=green]
publicObjectprocessObjectValue(Stringkey,Objectvalue,JsonConfigjsonConfig){
if(valueinstanceofDate){
Stringstr=newSimpleDateFormat(format).format((Date)value);
returnstr;
}
returnvalue==null?null:value.toString();
}
publicStringgetFormat(){
returnformat;
}
publicvoidsetFormat(Stringformat){
this.format=format;
}
}
[/color]