Json的转换
package com.utils; import java.io.IOException; import java.util.List; import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.JsonProcessingException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.type.JavaType; /** * 自定义响应结构 */ public class JsonUtils { // 定义jackson对象 private static final ObjectMapper MAPPER = new ObjectMapper(); /** * 将对象转换成json字符串。 * * @param data * @return */ public static String objectToJson(Object data) throws Exception { try { String string = MAPPER.writeValueAsString(data); return string; } catch (JsonProcessingException e) { e.printStackTrace(); } return null; } /** * 将json结果集转化为对象 * * @param jsonData * json数据 * 对象中的object类型 * @return */ public static <T> T jsonToPojo(String jsonData, Class<T> beanType) { try { T t = MAPPER.readValue(jsonData, beanType); return t; } catch (Exception e) { } return null; } /** * 将json数据转换成pojo对象list * * @param jsonData * @param beanType * @return */ public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) { JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType); try { List<T> list = MAPPER.readValue(jsonData, javaType); return list; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 将json结果集转化为对象 * * @param jsonData * json数据 * 对象中的object类型 * @return * @throws IOException * @throws JsonMappingException * @throws JsonParseException */ public static <T> T jsonToPojo1(String jsonData, Class<T> beanType) throws JsonParseException, JsonMappingException, IOException { T t = MAPPER.readValue(jsonData, beanType); return t; } /** * 把一个字符串转换成bean对象 * @param str * @param <T> * @return */ public static <T> T stringToBean(String str, Class<T> clazz) { if(str == null || str.length() <= 0 || clazz == null) { return null; } if(clazz == int.class || clazz == Integer.class) { return (T)Integer.valueOf(str); }else if(clazz == String.class) { return (T)str; }else if(clazz == long.class || clazz == Long.class) { return (T)Long.valueOf(str); }else { return com.alibaba.fastjson.JSON.toJavaObject(com.alibaba.fastjson.JSON.parseObject(str), clazz); } } }