package org.jeecgframework.core.util;@b@@b@import java.io.IOException;@b@import java.util.ArrayList;@b@import java.util.HashMap;@b@import java.util.Iterator;@b@import java.util.List;@b@import java.util.Map;@b@@b@import net.sf.json.JSONArray;@b@import net.sf.json.JSONObject;@b@@b@import org.apache.commons.beanutils.BeanUtils;@b@import org.codehaus.jackson.JsonParseException;@b@import org.codehaus.jackson.map.JsonMappingException;@b@import org.codehaus.jackson.map.ObjectMapper;@b@import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@@b@/**@b@ * JSON和JAVA的POJO的相互转换@b@ * @author 张代浩@b@ * JSONHelper.java@b@ */@b@public final class JSONHelper {@b@ private static final Logger logger = LoggerFactory.getLogger(JSONHelper.class);@b@@b@ // 将数组转换成JSON@b@ public static String array2json(Object object) {@b@ JSONArray jsonArray = JSONArray.fromObject(object);@b@ return jsonArray.toString();@b@ }@b@@b@ // 将JSON转换成数组,其中valueClz为数组中存放的对象的Class@b@ public static Object json2Array(String json, Class valueClz) {@b@ JSONArray jsonArray = JSONArray.fromObject(json);@b@ return JSONArray.toArray(jsonArray, valueClz);@b@ }@b@@b@ // 将Collection转换成JSON@b@ public static String collection2json(Object object) {@b@ JSONArray jsonArray = JSONArray.fromObject(object);@b@ return jsonArray.toString();@b@ }@b@@b@ // 将Map转换成JSON@b@ public static String map2json(Object object) {@b@ JSONObject jsonObject = JSONObject.fromObject(object);@b@ return jsonObject.toString();@b@ }@b@@b@ // 将JSON转换成Map,其中valueClz为Map中value的Class,keyArray为Map的key@b@ public static Map json2Map(Object[] keyArray, String json, Class valueClz) {@b@ JSONObject jsonObject = JSONObject.fromObject(json);@b@ Map classMap = new HashMap();@b@@b@ for (int i = 0; i < keyArray.length; i++) {@b@ classMap.put(keyArray[i], valueClz);@b@ }@b@@b@ return (Map) JSONObject.toBean(jsonObject, Map.class, classMap);@b@ }@b@@b@ // 将POJO转换成JSON@b@ public static String bean2json(Object object) {@b@ JSONObject jsonObject = JSONObject.fromObject(object);@b@ return jsonObject.toString();@b@ }@b@@b@ // 将JSON转换成POJO,其中beanClz为POJO的Class@b@ public static Object json2Object(String json, Class beanClz) {@b@ return JSONObject.toBean(JSONObject.fromObject(json), beanClz);@b@ }@b@@b@ /**@b@ * json转换为java对象@b@ * @b@ * <pre>@b@ * return JackJson.fromJsonToObject(this.answersJson, JackJson.class);@b@ * </pre>@b@ * @b@ * @param <T>@b@ * 要转换的对象@b@ * @param json@b@ * 字符串@b@ * @param valueType@b@ * 对象的class@b@ * @return 返回对象@b@ */@b@ public static <T> T fromJsonToObject(String json, Class<T> valueType) {@b@ ObjectMapper mapper = new ObjectMapper();@b@ try {@b@ return mapper.readValue(json, valueType);@b@ } catch (JsonParseException e) {@b@ logger.error("JsonParseException: ", e);@b@ } catch (JsonMappingException e) {@b@ logger.error("JsonMappingException: ", e);@b@ } catch (IOException e) {@b@ logger.error("IOException: ", e);@b@ }@b@ return null;@b@ }@b@@b@ // 将String转换成JSON@b@ public static String string2json(String key, String value) {@b@ JSONObject object = new JSONObject();@b@ object.put(key, value);@b@ return object.toString();@b@ }@b@@b@ // 将JSON转换成String@b@ public static String json2String(String json, String key) {@b@ JSONObject jsonObject = JSONObject.fromObject(json);@b@ return jsonObject.get(key).toString();@b@ }@b@@b@ /***@b@ * 将List对象序列化为JSON文本@b@ */@b@ public static <T> String toJSONString(List<T> list) {@b@ JSONArray jsonArray = JSONArray.fromObject(list);@b@@b@ return jsonArray.toString();@b@ }@b@@b@ /***@b@ * 将对象序列化为JSON文本@b@ * @b@ * @param object@b@ * @return@b@ */@b@ public static String toJSONString(Object object) {@b@ JSONArray jsonArray = JSONArray.fromObject(object);@b@@b@ return jsonArray.toString();@b@ }@b@@b@ /***@b@ * 将JSON对象数组序列化为JSON文本@b@ * @b@ * @param jsonArray@b@ * @return@b@ */@b@ public static String toJSONString(JSONArray jsonArray) {@b@ return jsonArray.toString();@b@ }@b@@b@ /***@b@ * 将JSON对象序列化为JSON文本@b@ * @b@ * @param jsonObject@b@ * @return@b@ */@b@ public static String toJSONString(JSONObject jsonObject) {@b@ return jsonObject.toString();@b@ }@b@@b@ /***@b@ * 将对象转换为List对象@b@ * @b@ * @param object@b@ * @return@b@ */@b@ public static List toArrayList(Object object) {@b@ List arrayList = new ArrayList();@b@@b@ JSONArray jsonArray = JSONArray.fromObject(object);@b@@b@ Iterator it = jsonArray.iterator();@b@ while (it.hasNext()) {@b@ JSONObject jsonObject = (JSONObject) it.next();@b@@b@ Iterator keys = jsonObject.keys();@b@ while (keys.hasNext()) {@b@ Object key = keys.next();@b@ Object value = jsonObject.get(key);@b@ arrayList.add(value);@b@ }@b@ }@b@@b@ return arrayList;@b@ }@b@@b@ /* *//***@b@ * 将对象转换为Collection对象@b@ * @b@ * @param object@b@ * @return@b@ */@b@ /*@b@ * public static Collection toCollection(Object object) { JSONArray@b@ * jsonArray = JSONArray.fromObject(object);@b@ * @b@ * return JSONArray.toCollection(jsonArray); }@b@ */@b@@b@ /***@b@ * 将对象转换为JSON对象数组@b@ * @b@ * @param object@b@ * @return@b@ */@b@ public static JSONArray toJSONArray(Object object) {@b@ return JSONArray.fromObject(object);@b@ }@b@@b@ /***@b@ * 将对象转换为JSON对象@b@ * @b@ * @param object@b@ * @return@b@ */@b@ public static JSONObject toJSONObject(Object object) {@b@ return JSONObject.fromObject(object);@b@ }@b@@b@ /***@b@ * 将对象转换为HashMap@b@ * @b@ * @param object@b@ * @return@b@ */@b@ public static HashMap toHashMap(Object object) {@b@ HashMap<String, Object> data = new HashMap<String, Object>();@b@ JSONObject jsonObject = JSONHelper.toJSONObject(object);@b@ Iterator it = jsonObject.keys();@b@ while (it.hasNext()) {@b@ String key = String.valueOf(it.next());@b@ Object value = jsonObject.get(key);@b@ data.put(key, value);@b@ }@b@@b@ return data;@b@ }@b@ @b@ /** @b@ * 将json格式的字符串解析成Map对象 <li> @b@ * json格式:{"name":"admin","retries":"3fff","testname":"ddd","testretries":"fffffffff"} @b@ */ @b@ public static Map<String, Object> json2Map(String jsonStr) { @b@ Map<String, Object> data = new HashMap<String, Object>(); @b@ // 将json字符串转换成jsonObject @b@ JSONObject jsonObject = JSONObject.fromObject(jsonStr); @b@ Iterator it = jsonObject.keys(); @b@ // 遍历jsonObject数据,添加到Map对象 @b@ while (it.hasNext()) @b@ { @b@ String key = String.valueOf(it.next()); @b@ Object value = jsonObject.get(key); @b@ data.put(key, value); @b@ } @b@ return data; @b@ } @b@ @b@ @b@ /** @b@ * 将json格式的字符串解析成Map对象 <li> @b@ * json格式:{"name":"admin","retries":"3fff","testname":"ddd","testretries":"fffffffff"} @b@ */ @b@ public static Map<String, List<Map<String, Object>>> json2MapList(String jsonStr) { @b@ Map<String, List<Map<String, Object>>> data = new HashMap<String, List<Map<String, Object>>>(); @b@ // 将json字符串转换成jsonObject @b@ JSONObject jsonObject = JSONObject.fromObject(jsonStr); @b@ Iterator it = jsonObject.keys(); @b@ // 遍历jsonObject数据,添加到Map对象 @b@ while (it.hasNext()) @b@ { @b@ String key = String.valueOf(it.next()); @b@ Object value = jsonObject.get(key); @b@ List<Map<String, Object>> list = toList(value);@b@ data.put(key, list); @b@ } @b@ return data; @b@ } @b@@b@ /***@b@ * 将对象转换为List<Map<String,Object>>@b@ * @b@ * @param object@b@ * @return@b@ */@b@ // 返回非实体类型(Map<String,Object>)的List@b@ public static List<Map<String, Object>> toList(Object object) {@b@ List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();@b@ JSONArray jsonArray = JSONArray.fromObject(object);@b@ for (Object obj : jsonArray) {@b@ JSONObject jsonObject = (JSONObject) obj;@b@ Map<String, Object> map = new HashMap<String, Object>();@b@ Iterator it = jsonObject.keys();@b@ while (it.hasNext()) {@b@ String key = (String) it.next();@b@ Object value = jsonObject.get(key);@b@ map.put((String) key, value);@b@ }@b@ list.add(map);@b@ }@b@ return list;@b@ }@b@ @b@ // 返回非实体类型(Map<String,Object>)的List@b@ public static List<Map<String, Object>> toList(JSONArray jsonArray) {@b@ List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();@b@ for (Object obj : jsonArray) {@b@ JSONObject jsonObject = (JSONObject) obj;@b@ Map<String, Object> map = new HashMap<String, Object>();@b@ Iterator it = jsonObject.keys();@b@ while (it.hasNext()) {@b@ String key = (String) it.next();@b@ Object value = jsonObject.get(key);@b@ map.put((String) key, value);@b@ }@b@ list.add(map);@b@ }@b@ return list;@b@ }@b@@b@ /***@b@ * 将JSON对象数组转换为传入类型的List@b@ * @b@ * @param <T>@b@ * @param jsonArray@b@ * @param objectClass@b@ * @return@b@ */@b@ public static <T> List<T> toList(JSONArray jsonArray, Class<T> objectClass) {@b@ return JSONArray.toList(jsonArray, objectClass);@b@ }@b@@b@ /***@b@ * 将对象转换为传入类型的List@b@ * @b@ * @param <T>@b@ * @param jsonArray@b@ * @param objectClass@b@ * @return@b@ */@b@ public static <T> List<T> toList(Object object, Class<T> objectClass) {@b@ JSONArray jsonArray = JSONArray.fromObject(object);@b@@b@ return JSONArray.toList(jsonArray, objectClass);@b@ }@b@@b@ /***@b@ * 将JSON对象转换为传入类型的对象@b@ * @b@ * @param <T>@b@ * @param jsonObject@b@ * @param beanClass@b@ * @return@b@ */@b@ public static <T> T toBean(JSONObject jsonObject, Class<T> beanClass) {@b@ return (T) JSONObject.toBean(jsonObject, beanClass);@b@ }@b@@b@ /***@b@ * 将将对象转换为传入类型的对象@b@ * @b@ * @param <T>@b@ * @param object@b@ * @param beanClass@b@ * @return@b@ */@b@ public static <T> T toBean(Object object, Class<T> beanClass) {@b@ JSONObject jsonObject = JSONObject.fromObject(object);@b@@b@ return (T) JSONObject.toBean(jsonObject, beanClass);@b@ }@b@@b@ /***@b@ * 将JSON文本反序列化为主从关系的实体@b@ * @b@ * @param <T>@b@ * 泛型T 代表主实体类型@b@ * @param <D>@b@ * 泛型D 代表从实体类型@b@ * @param jsonString@b@ * JSON文本@b@ * @param mainClass@b@ * 主实体类型@b@ * @param detailName@b@ * 从实体类在主实体类中的属性名称@b@ * @param detailClass@b@ * 从实体类型@b@ * @return@b@ */@b@ public static <T, D> T toBean(String jsonString, Class<T> mainClass,@b@ String detailName, Class<D> detailClass) {@b@ JSONObject jsonObject = JSONObject.fromObject(jsonString);@b@ JSONArray jsonArray = (JSONArray) jsonObject.get(detailName);@b@@b@ T mainEntity = JSONHelper.toBean(jsonObject, mainClass);@b@ List<D> detailList = JSONHelper.toList(jsonArray, detailClass);@b@@b@ try {@b@ BeanUtils.setProperty(mainEntity, detailName, detailList);@b@ } catch (Exception ex) {@b@ throw new RuntimeException("主从关系JSON反序列化实体失败!");@b@ }@b@@b@ return mainEntity;@b@ }@b@@b@ /***@b@ * 将JSON文本反序列化为主从关系的实体@b@ * @b@ * @param <T>泛型T 代表主实体类型@b@ * @param <D1>泛型D1 代表从实体类型@b@ * @param <D2>泛型D2 代表从实体类型@b@ * @param jsonString@b@ * JSON文本@b@ * @param mainClass@b@ * 主实体类型@b@ * @param detailName1@b@ * 从实体类在主实体类中的属性@b@ * @param detailClass1@b@ * 从实体类型@b@ * @param detailName2@b@ * 从实体类在主实体类中的属性@b@ * @param detailClass2@b@ * 从实体类型@b@ * @return@b@ */@b@ public static <T, D1, D2> T toBean(String jsonString, Class<T> mainClass,@b@ String detailName1, Class<D1> detailClass1, String detailName2,@b@ Class<D2> detailClass2) {@b@ JSONObject jsonObject = JSONObject.fromObject(jsonString);@b@ JSONArray jsonArray1 = (JSONArray) jsonObject.get(detailName1);@b@ JSONArray jsonArray2 = (JSONArray) jsonObject.get(detailName2);@b@@b@ T mainEntity = JSONHelper.toBean(jsonObject, mainClass);@b@ List<D1> detailList1 = JSONHelper.toList(jsonArray1, detailClass1);@b@ List<D2> detailList2 = JSONHelper.toList(jsonArray2, detailClass2);@b@@b@ try {@b@ BeanUtils.setProperty(mainEntity, detailName1, detailList1);@b@ BeanUtils.setProperty(mainEntity, detailName2, detailList2);@b@ } catch (Exception ex) {@b@ throw new RuntimeException("主从关系JSON反序列化实体失败!");@b@ }@b@@b@ return mainEntity;@b@ }@b@@b@ /***@b@ * 将JSON文本反序列化为主从关系的实体@b@ * @b@ * @param <T>泛型T 代表主实体类型@b@ * @param <D1>泛型D1 代表从实体类型@b@ * @param <D2>泛型D2 代表从实体类型@b@ * @param jsonString@b@ * JSON文本@b@ * @param mainClass@b@ * 主实体类型@b@ * @param detailName1@b@ * 从实体类在主实体类中的属性@b@ * @param detailClass1@b@ * 从实体类型@b@ * @param detailName2@b@ * 从实体类在主实体类中的属性@b@ * @param detailClass2@b@ * 从实体类型@b@ * @param detailName3@b@ * 从实体类在主实体类中的属性@b@ * @param detailClass3@b@ * 从实体类型@b@ * @return@b@ */@b@ public static <T, D1, D2, D3> T toBean(String jsonString,@b@ Class<T> mainClass, String detailName1, Class<D1> detailClass1,@b@ String detailName2, Class<D2> detailClass2, String detailName3,@b@ Class<D3> detailClass3) {@b@ JSONObject jsonObject = JSONObject.fromObject(jsonString);@b@ JSONArray jsonArray1 = (JSONArray) jsonObject.get(detailName1);@b@ JSONArray jsonArray2 = (JSONArray) jsonObject.get(detailName2);@b@ JSONArray jsonArray3 = (JSONArray) jsonObject.get(detailName3);@b@@b@ T mainEntity = JSONHelper.toBean(jsonObject, mainClass);@b@ List<D1> detailList1 = JSONHelper.toList(jsonArray1, detailClass1);@b@ List<D2> detailList2 = JSONHelper.toList(jsonArray2, detailClass2);@b@ List<D3> detailList3 = JSONHelper.toList(jsonArray3, detailClass3);@b@@b@ try {@b@ BeanUtils.setProperty(mainEntity, detailName1, detailList1);@b@ BeanUtils.setProperty(mainEntity, detailName2, detailList2);@b@ BeanUtils.setProperty(mainEntity, detailName3, detailList3);@b@ } catch (Exception ex) {@b@ throw new RuntimeException("主从关系JSON反序列化实体失败!");@b@ }@b@@b@ return mainEntity;@b@ }@b@@b@ /***@b@ * 将JSON文本反序列化为主从关系的实体@b@ * @b@ * @param <T>@b@ * 主实体类型@b@ * @param jsonString@b@ * JSON文本@b@ * @param mainClass@b@ * 主实体类型@b@ * @param detailClass@b@ * 存放了多个从实体在主实体中属性名称和类型@b@ * @return@b@ */@b@ public static <T> T toBean(String jsonString, Class<T> mainClass,@b@ HashMap<String, Class> detailClass) {@b@ JSONObject jsonObject = JSONObject.fromObject(jsonString);@b@ T mainEntity = JSONHelper.toBean(jsonObject, mainClass);@b@ for (Object key : detailClass.keySet()) {@b@ try {@b@ Class value = (Class) detailClass.get(key);@b@ BeanUtils.setProperty(mainEntity, key.toString(), value);@b@ } catch (Exception ex) {@b@ throw new RuntimeException("主从关系JSON反序列化实体失败!");@b@ }@b@ }@b@ return mainEntity;@b@ }@b@ @b@ public static String listtojson(String[] fields, int total, List list) throws Exception {@b@ Object[] values = new Object[fields.length];@b@ String jsonTemp = "{\"total\":" + total + ",\"rows\":[";@b@ for (int j = 0; j < list.size(); j++) {@b@ jsonTemp = jsonTemp + "{\"state\":\"closed\",";@b@ for (int i = 0; i < fields.length; i++) {@b@ String fieldName = fields[i].toString();@b@ values[i] = org.jeecgframework.tag.core.easyui.TagUtil.fieldNametoValues(fieldName, list.get(j));@b@ jsonTemp = jsonTemp + "\"" + fieldName + "\"" + ":\"" + values[i] + "\"";@b@ if (i != fields.length - 1) {@b@ jsonTemp = jsonTemp + ",";@b@ }@b@ }@b@ if (j != list.size() - 1) {@b@ jsonTemp = jsonTemp + "},";@b@ } else {@b@ jsonTemp = jsonTemp + "}";@b@ }@b@ }@b@ jsonTemp = jsonTemp + "]}";@b@ return jsonTemp;@b@ }@b@@b@}