1、code
package top.zywork.common.utils;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.json.Json;
public class JsonUtils {
/**
* 使用JSONObject把json转换成bean
*
* @param jsonStr 需要转成对象的json字符串
* @param obj 需要转成的对象
* @return
*/
public static Object jsonToBeanByJSONObject(String jsonStr, Class<?> obj) {
try {
return JSONObject.parseObject(jsonStr, obj);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
/**
* 使用JSONObject把对象转json
*
* @param obj 需要转成json字符串的对象
* @return
*/
public static String objectToJsonByJSONObject(Object obj) {
try {
return JSONObject.toJSONString(obj);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
/**
* 使用JSON把json转换成bean
*
* @param strJson 需要转成对象的json字符串
* @param obj 需要转成的对象
* @return
*/
public static Object jsonToBeanByJSON(String jsonStr, Class<?> obj) {
try {
return Json.getJson().parse(jsonStr, obj);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
/**
* 对象转成json字符串
*
* @param object 任意对象
* @return
*/
public static String objectToJson(Object object) {
StringBuilder json = new StringBuilder();
if (object == null) {
json.append("\"\"");
} else if (object instanceof String || object instanceof Integer) {
json.append("\"").append(object.toString()).append("\"");
} else {
json.append(beanToJson(object));
}
return json.toString();
}
/**
* 传入任意一个 javabean 对象生成一个指定规格的json字符串
*
* @param bean bean对象
* @return String
*/
public static String beanToJson(Object bean) {
StringBuilder json = new StringBuilder();
json.append("{");
PropertyDescriptor[] props = null;
try {
props = Introspector.getBeanInfo(bean.getClass(), Object.class)
.getPropertyDescriptors();
} catch (IntrospectionException e) {
}
if (props != null) {
for (int i = 0; i < props.length; i++) {
try {
String name = objectToJson(props[i].getName());
String value = objectToJson(props[i].getReadMethod().invoke(bean));
json.append(name);
json.append(":");
json.append(value);
json.append(",");
} catch (Exception e) {
}
}
json.setCharAt(json.length() - 1, '}');
} else {
json.append("}");
}
return json.toString();
}
/**
* 功能描述:通过传入一个列表对象,调用指定方法将列表中的数据生成一个JSON规格指定字符串
*
* @param list 列表对象
* @return
*/
public static String listToJson(List<?> list) {
StringBuilder json = new StringBuilder();
json.append("[");
if (list != null && list.size() > 0) {
for (Object obj : list) {
json.append(objectToJson(obj));
json.append(",");
}
json.setCharAt(json.length() - 1, ']');
} else {
json.append("]");
}
return json.toString();
}
/**
* 取出json中的值
*
* @param jsonStr 格式“{value,value}”
* @return
* @throws JSONException
*/
public static Object[] getObjectArraySon(String jsonStr) {
String a = "";
JSONArray json;
Object[] s = null;
try {
json = new JSONArray(jsonStr);
for (int i = 0; i < json.length(); i++) {
Object jo = json.getJSONObject(i);
s[i] = jo;
}
} catch (JSONException e) {
a = "0";
}
return s;
}
/**
* 用fastjson将数据转换为map的形式,值为Object
*
* @param jsonStr
* @return
*/
public static Map<String, Object> jsonToMapValObj(String jsonStr) {
Map<String, Object> map = new HashMap<String, Object>();
com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(jsonStr);
for (java.util.Map.Entry<String, Object> entry : jsonObject.entrySet()) {
map.put(entry.getKey(), entry.getValue());
}
return map;
}
/**
* map转json,map的值为String格式
*
* @param map
* @return
*/
public static String mapToJsonValStr(Map<String, String> map) {
String resultJson = JSONObject.toJSONString(map);
return resultJson;
}
}