package org.platform.utils.json;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class JSONHelper {
@SuppressWarnings("unused")
private static final Logger logger = LoggerFactory
.getLogger(JSONHelper.class);
// 将数组转换成JSON
public static String array2json(Object object) {
JSONArray jsonArray = JSONArray.fromObject(object);
return jsonArray.toString();
}
// 将JSON转换成数组,其中valueClz为数组中存放的对象的Class
@SuppressWarnings("rawtypes")
public static Object json2Array(String json, Class valueClz) {
JSONArray jsonArray = JSONArray.fromObject(json);
return JSONArray.toArray(jsonArray, valueClz);
}
// 将Collection转换成JSON
public static String collection2json(Object object) {
JSONArray jsonArray = JSONArray.fromObject(object);
return jsonArray.toString();
}
// 将Map转换成JSON
public static String map2json(Object object) {
JSONObject jsonObject = JSONObject.fromObject(object);
return jsonObject.toString();
}
// 将JSON转换成Map,其中valueClz为Map中value的Class,keyArray为Map的key
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Map json2Map(Object[] keyArray, String json, Class valueClz) {
JSONObject jsonObject = JSONObject.fromObject(json);
Map classMap = new HashMap();
for (int i = 0; i < keyArray.length; i++) {
classMap.put(keyArray[i], valueClz);
}
return (Map) JSONObject.toBean(jsonObject, Map.class, classMap);
}
// 将POJO转换成JSON
public static String bean2json(Object object) {
JSONObject jsonObject = JSONObject.fromObject(object);
return jsonObject.toString();
}
// 将JSON转换成POJO,其中beanClz为POJO的Class
@SuppressWarnings("rawtypes")
public static Object json2Object(String json, Class beanClz) {
return JSONObject.toBean(JSONObject.fromObject(json), beanClz);
}
// 将String转换成JSON
public static String string2json(String key, String value) {
JSONObject object = new JSONObject();
object.put(key, value);
return object.toString();
}
// 将JSON转换成String
public static String json2String(String json, String key) {
JSONObject jsonObject = JSONObject.fromObject(json);
return jsonObject.get(key).toString();
}
/***
* 将List对象序列化为JSON文本
*/
public static String toJSONString(List list) {
JSONArray jsonArray = JSONArray.fromObject(list);
return jsonArray.toString();
}
/***
* 将对象序列化为JSON文本
*
* @param object
* @return
*/
public static String toJSONString(Object object) {
JSONArray jsonArray = JSONArray.fromObject(object);
return jsonArray.toString();
}
/***
* 将JSON对象数组序列化为JSON文本
*
* @param jsonArray
* @return
*/
public static String toJSONString(JSONArray jsonArray) {
return jsonArray.toString();
}
/***
* 将JSON对象序列化为JSON文本
*
* @param jsonObject
* @return
*/
public static String toJSONString(JSONObject jsonObject) {
return jsonObject.toString();
}
/***
* 将对象转换为List对象
*
* @param object
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List toArrayList(Object object) {
List arrayList = new ArrayList();
JSONArray jsonArray = JSONArray.fromObject(object);
Iterator it = jsonArray.iterator();
while (it.hasNext()) {
JSONObject jsonObject = (JSONObject) it.next();
Iterator keys = jsonObject.keys();
while (keys.hasNext()) {
Object key = keys.next();
Object value = jsonObject.get(key);
arrayList.add(value);
}
}
return arrayList;
}
/* *//***
* 将对象转换为Collection对象
*
* @param object
* @return
*/
/*
* public static Collection toCollection(Object object) { JSONArray
* jsonArray = JSONArray.fromObject(object);
*
* return JSONArray.toCollection(jsonArray); }
*/
/***
* 将对象转换为JSON对象数组
*
* @param object
* @return
*/
public static JSONArray toJSONArray(Object object) {
return JSONArray.fromObject(object);
}
/***
* 将对象转换为JSON对象
*
* @param object
* @return
*/
public static JSONObject toJSONObject(Object object) {
return JSONObject.fromObject(object);
}
/***
* 将对象转换为HashMap
*
* @param object
* @return
*/
@SuppressWarnings("rawtypes")
public static HashMap toHashMap(Object object) {
HashMap data = new HashMap();
JSONObject jsonObject = JSONHelper.toJSONObject(object);
Iterator it = jsonObject.keys();
while (it.hasNext()) {
String key = String.valueOf(it.next());
Object value = jsonObject.get(key);
data.put(key, value);
}
return data;
}
/**
* 将json格式的字符串解析成Map对象
* json格式:{"name":"admin","retries":"3fff","testname":"ddd","testretries":
* "fffffffff"}
*/
@SuppressWarnings("rawtypes")
public static Map json2Map(String jsonStr) {
Map data = new HashMap();
// 将json字符串转换成jsonObject
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
Iterator it = jsonObject.keys();
// 遍历jsonObject数据,添加到Map对象
while (it.hasNext()) {
String key = String.valueOf(it.next());
Object value = jsonObject.get(key);
data.put(key, value);
}
return data;
}
/**
* 将json格式的字符串解析成Map对象
* json格式:{"name":"admin","retries":"3fff","testname":"ddd","testretries":
* "fffffffff"}
*/
@SuppressWarnings("rawtypes")
public static Map>> json2MapList(
String jsonStr) {
Map>> data = new HashMap>>();
// 将json字符串转换成jsonObject
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
Iterator it = jsonObject.keys();
// 遍历jsonObject数据,添加到Map对象
while (it.hasNext()) {
String key = String.valueOf(it.next());
Object value = jsonObject.get(key);
List