json相关

一、GSON操作
import com.google.gson.Gson;

ContractDTO contract = null;
try {
Map result = quotationInfoDAO.queryQuoteDataByQuotationNo(quotationNo);
if (CommonFunctions.isMapEmpty(result))
return contract;
String latestQuoteData = (String) result.get("latestQuoteData");
//JSONObject json = JSONObject.fromObject(latestQuoteData);
//contract = (ContractDTO) JSONObject.toBean(json, ContractDTO.class);
Gson gson = new Gson();
contract = gson.fromJson(latestQuoteData, ContractDTO.class);
} catch (Exception ex) {
LogUtils.error("查询财意报价单clob异常,参数quotationNo:" + quotationNo, ex);
}

二、json
package com.paic.icore.acss.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.ezmorph.Morpher;
import net.sf.ezmorph.MorpherRegistry;
import net.sf.ezmorph.bean.BeanMorpher;
import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.JSONUtils;

import com.paic.icore.acss.util.enumeration.DateStyle;

/**
* Json转换工具类
* @author ZHANGYUNPENG048
*
*/
public class JsonUtils {
/**
* 从一个JSON 对象字符格式中得到一个java对象,形如:
* {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...}}
* @param object
* @param clazz
* @return
*/
public static Object getDTO(String jsonString, Class clazz) {
JSONObject jsonObject = null;
setDateFormat2JAVA();
jsonObject = JSONObject.fromObject(jsonString);
return JSONObject.toBean(jsonObject, clazz);
}

/**
* 从一个JSON 对象字符格式中得到一个java对象,其中beansList是一类的集合,形如:
* {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...},
* beansList:[{}, {}, ...]}
* @param jsonString
* @param clazz
* @param map 集合属性的类型 (key : 集合属性名, value : 集合属性类型class) eg: ("beansList" : Bean.class)
* @return
*/
public static Object getDTO(String jsonString, Class clazz, Map map) {
JSONObject jsonObject = null;
setDateFormat2JAVA();
jsonObject = JSONObject.fromObject(jsonString);
return JSONObject.toBean(jsonObject, clazz, map);
}

/**
* 从一个JSON数组得到一个java对象数组,形如:
* [{"id" : idValue, "name" : nameValue}, {"id" : idValue, "name" : nameValue}, ...]
* @param object
* @param clazz
* @return
*/
public static Object[] getDTOArray(String jsonString, Class clazz) {
setDateFormat2JAVA();
JSONArray array = JSONArray.fromObject(jsonString);
Object[] obj = new Object[array.size()];
for (int i = 0; i < array.size(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
obj[i] = JSONObject.toBean(jsonObject, clazz);
}
return obj;
}

/**
* 从一个JSON数组得到一个java对象数组,形如:
* [{"id" : idValue, "name" : nameValue}, {"id" : idValue, "name" : nameValue}, ...]
* @param object
* @param clazz
* @param map
* @return
*/
public static Object[] getDTOArray(String jsonString, Class clazz, Map map) {
setDateFormat2JAVA();
JSONArray array = JSONArray.fromObject(jsonString);
Object[] obj = new Object[array.size()];
for (int i = 0; i < array.size(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
obj[i] = JSONObject.toBean(jsonObject, clazz, map);
}
return obj;
}

/**
* 从一个JSON数组得到一个java对象集合
* @param object
* @param clazz
* @return
*/
public static List getDTOList(String jsonString, Class clazz) {
setDateFormat2JAVA();
JSONArray array = JSONArray.fromObject(jsonString);
List list = new ArrayList();
for (Iterator iter = array.iterator(); iter.hasNext();) {
JSONObject jsonObject = (JSONObject) iter.next();
list.add(JSONObject.toBean(jsonObject, clazz));
}
return list;
}

/**
* 从一个JSON数组得到一个java对象集合,其中对象中包含有集合属性
* @param object
* @param clazz
* @param map 集合属性的类型
* @return
*/
public static List getDTOList(String jsonString, Class clazz, Map map) {
setDateFormat2JAVA();
JSONArray array = JSONArray.fromObject(jsonString);
List list = new ArrayList();
for (Iterator iter = array.iterator(); iter.hasNext();) {
JSONObject jsonObject = (JSONObject) iter.next();
list.add(JSONObject.toBean(jsonObject, clazz, map));
}
return list;
}

/**
* 从json HASH表达式中获取一个map,该map支持嵌套功能
* 形如:{"id" : "johncon", "name" : "小强"}
* 注意commons-collections版本,必须包含org.apache.commons.collections.map.MultiKeyMap
* @param object
* @return
*/
public static Map getMapFromJson(String jsonString) {
setDateFormat2JAVA();
JSONObject jsonObject = JSONObject.fromObject(jsonString);
Map map = new HashMap();
for (Iterator iter = jsonObject.keys(); iter.hasNext();) {
String key = (String) iter.next();
map.put(key, jsonObject.get(key));
}
return map;
}

/**
* 从json数组中得到相应java数组
* json形如:["123", "456"]
* @param jsonString
* @return
*/
public static Object[] getArrayFromJson(String jsonString) {
JSONArray jsonArray = JSONArray.fromObject(jsonString);
return jsonArray.toArray();
}

/**
* 把数据对象转换成json字符串
* DTO对象形如:{"id" : idValue, "name" : nameValue, ...}
* 数组对象形如:[{}, {}, {}, ...]
* map对象形如:{key1 : {"id" : idValue, "name" : nameValue, ...}, key2 : {}, ...}
* @param object
* @return
*/
public static String getJSONString(Object object) throws Exception {
String jsonString = null;
//日期值处理器
JsonConfig jsonConfig = new JsonConfig();
JsonValueProcessor jsonValueProcessor = new JsonDateValueProcessor();
jsonConfig.registerJsonValueProcessor(java.util.Date.class, jsonValueProcessor);

if (object != null) {
if (object instanceof Collection || object instanceof Object[]) {
jsonString = JSONArray.fromObject(object, jsonConfig).toString();
} else {
jsonString = JSONObject.fromObject(object, jsonConfig).toString();
}
}
return jsonString == null ? "{}" : jsonString;
}

设定日期转换格式
private static void setDateFormat2JAVA() {
DateMorpher dmp = new DateMorpher(new String[] { DateStyle.DATE_STYLE.value(),
DateStyle.FULL_DATE_STYLE.value() });
JSONUtils.getMorpherRegistry().registerMorpher(dmp);
}

/**
* 使用注册器对指定DynaBean进行对象变换
*/
public static Object getMorphTargetObject(Class targetClazz, Object srcJsonBeanMorpherObject) {
MorpherRegistry morpherRegistry = JSONUtils.getMorpherRegistry();
Morpher dynaMorpher = new BeanMorpher(targetClazz, morpherRegistry);
morpherRegistry.registerMorpher(dynaMorpher);
return morpherRegistry.morph(targetClazz, srcJsonBeanMorpherObject);
}

}

/*
* json日期值处理器
*/
class JsonDateValueProcessor implements JsonValueProcessor {
private String format = DateStyle.FULL_DATE_STYLE.value();

public JsonDateValueProcessor() {
}

public Object processArrayValue(Object value, JsonConfig jsonConfig) {
return process(value, jsonConfig);
}

public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
return process(value, jsonConfig);
}

private Object process(Object value, JsonConfig jsonConfig) {
if (value instanceof Date) {
return new SimpleDateFormat(format).format((Date) value);
}
return value == null ? null : value.toString();
}

public String getFormat() {
return format;
}

public void setFormat(String format) {
this.format = format;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值