}
/**
* 把json字符串转成Object对象
* @param jsonString
* @return T
*/
public static <T> T parseJsonToObject(String jsonString, Class<T> valueType) {
if(jsonString == null || "".equals((jsonString))){
return null;
}
try {
return objectMapper.readValue(jsonString, valueType);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 把json字符串转成List对象
* @param jsonString
* @return List<T>
*/
@SuppressWarnings("unchecked")
public static <T> List<T> parseJsonToList(String jsonString,Class<T> valueType) {
if(jsonString == null || "".equals((jsonString))){
return null;
}
List<T> result = new ArrayList<T>();
try {
List<LinkedHashMap<Object, Object>> list = objectMapper.readValue(jsonString, List.class);
for (LinkedHashMap<Object, Object> map : list) {
String jsonStr = convertObjectToJson(map);
T t = parseJsonToObject(jsonStr, valueType);
result.add(t);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* JSON处理含有嵌套关系对象,避免出现异常:net.sf.json.JSONException: There is a cycle in the hierarchy的方法
* 注意:这样获得到的字符串中,引起嵌套循环的属性会置为null
*
* @param obj
* @return
*/
public static JSONObject getJsonObject(Object obj) {
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
@Override
public boolean apply(Object source, String name, Object value) {
if(value==null){
return true;
}
return false;
}
});
return JSONObject.fromObject(obj, jsonConfig);
}
/**
* JSON处理含有嵌套关系对象,避免出现异常:net.sf.json.JSONException: There is a cycle in the hierarchy的方法
* 注意:这样获得到的字符串中,引起嵌套循环的属性会置为null
*
* @param obj
* @return
*/
public static JSONArray getJsonArray(Object obj) {
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
return JSONArray.fromObject(obj, jsonConfig);
}
/**
* 解析JSON字符串成一个MAP
*
* @param jsonStr json字符串,格式如: {dictTable:"BM_XB",groupValue:"分组值"}
* @return
*/
public static Map<String, Object> parseJsonStr(String jsonStr) {
Map<String, Object> result = new HashMap<String, Object>();
JSONObject jsonObj = JsonUtil.getJsonObject(jsonStr);