packagefounder.util;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importcom.fasterxml.jackson.core.JsonParseException;importcom.fasterxml.jackson.core.JsonProcessingException;importcom.fasterxml.jackson.databind.JavaType;importcom.fasterxml.jackson.databind.JsonMappingException;importcom.fasterxml.jackson.databind.ObjectMapper;/*** @ClassName: JsonUtils
*@authorhanwl
* @date 2019年01月22日
* @Description: TODO*/
public classJsonUtils {private static final ObjectMapper OBJECT_MAPPER = newObjectMapper();/*** Json格式的字符串向JavaBean转换,传入空串将返回null
*@paramstrBody Json格式的字符串
*@paramc 目标JavaBean类型
*@returnJavaBean对象
*@throwsJsonParseException
*@throwsJsonMappingException
*@throwsIOException*/
public static T json2Object(String strBody, Class c) throwsJsonParseException, JsonMappingException, IOException{if (strBody == null || "".equals(strBody)) {return null;
}else{returnOBJECT_MAPPER.readValue(strBody, c);
}
}/*** Json格式的字符串向JavaBean转换,传入空串将返回null
*@paramstrBody Json格式的字符串
*@paramc 目标JavaBean类型
*@returnJavaBean对象, 如果解析失败返回 null*/
public static T decodeJson(String strBody, Classc) {if (strBody == null || "".equals(strBody)) {return null;
}else{try{returnOBJECT_MAPPER.readValue(strBody, c);
}catch(IOException e) {
e.printStackTrace();return null;
}
}
}/***
*@paramstrBody
*@paramc
*@return*@throwsJsonParseException
*@throwsJsonMappingException
*@throwsIOException*/
public static Object json2ComplexObject(String strBody) throwsJsonParseException, JsonMappingException, IOException{if (strBody == null || "".equals(strBody)) {return null;
}else{//每个属性的实际类型是string
return OBJECT_MAPPER.readValue(strBody, Object.class);
}
}/*** Json格式的字符串向JavaBean List集合转换,传入空串将返回null
*@paramstrBody
*@paramc
*@return*@throwsJsonParseException
*@throwsJsonMappingException
*@throwsIOException*/@SuppressWarnings("unchecked")public static List json2ObjectList(String strBody,Class c) throwsJsonParseException, JsonMappingException, IOException{if (strBody == null || "".equals(strBody)) {return null;
}else{
JavaType javaType= OBJECT_MAPPER.getTypeFactory().constructParametricType(ArrayList.class, c);return (List) OBJECT_MAPPER.readValue(strBody, javaType);
}
}/*** Json格式的字符串向JavaBean List集合转换,传入空串将返回null
*@paramstrBody
*@paramc
*@return对象列表,解析失败返回 null*/@SuppressWarnings("unchecked")public static List decodeJsonToList(String strBody,Classc) {if (strBody == null || "".equals(strBody)) {return null;
}else{
JavaType javaType= OBJECT_MAPPER.getTypeFactory().constructParametricType(ArrayList.class, c);try{return (List) OBJECT_MAPPER.readValue(strBody, javaType);
}catch(IOException e) {
e.printStackTrace();return null;
}
}
}/*** Json格式的字符串向List集合转换,传入空串将返回null
*@paramstrBody
*@return*@throwsJsonParseException
*@throwsJsonMappingException
*@throwsIOException*/
public static List json2List(String strBody) throwsJsonParseException, JsonMappingException, IOException{return json2ObjectList(strBody, String.class);
}/*** Object转为Json格式字符串的方法
*@paramo
*@return*@throwsJsonProcessingException*/
public static String object2Json(Object o) throwsJsonProcessingException{returnOBJECT_MAPPER.writeValueAsString(o);
}/*** Object转为Json格式字符串的方法
*@paramo
*@return对象的json字符串,如果处理过程中出错,返回null*/
public staticString encodeObject(Object o) {try{returnOBJECT_MAPPER.writeValueAsString(o);
}catch(JsonProcessingException e) {
e.printStackTrace();return null;
}
}
}

6万+

被折叠的 条评论
为什么被折叠?



