java fasterxml_com.fasterxml.jackson工具类

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;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值