Jackson處理一般的JavaBean和Json之間的轉換只要使用ObjectMapper 對象的readValue和writeValueAsString兩個方法就能實現(xiàn)。但是如果要轉換復雜類型Collection如 List<YourBean>,那么就需要先反序列化復雜類型 為泛型的Collection Type阎肝。
- 如果是ArrayList<YourBean>那么使用ObjectMapper 的getTypeFactory().constructParametricType(collectionClass, elementClasses);
- 如果是HashMap<String,YourBean>那么 ObjectMapper 的getTypeFactory().constructParametricType(HashMap.class,String.class, YourBean.class);
public final ObjectMapper mapper = new ObjectMapper();
public static void main(String[] args) throws Exception{
JavaType javaType = getCollectionType(ArrayList.class, YourBean.class);
List<YourBean> lst = (List<YourBean>)mapper.readValue(jsonString, javaType);
}
/**
* 獲取泛型的Collection Type
* @param collectionClass 泛型的Collection
* @param elementClasses 元素類
* @return JavaType Java類型
* @since 1.0
*/
public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}
我的代碼測試如下:
public class Object2JSON互轉 {
public static void main( String[] args ) throws IOException {
Student student = new Student();
student.setAccount("wukong");
student.setUserName("悟空");
//Jaskson工具類
ObjectMapper objectMapper = new ObjectMapper();
// 1.將Java對象轉換為JSON格式的字符串
String jsonStr = objectMapper.writeValueAsString(student);
System.out.println("Student轉JSON格式字符串:"+jsonStr);
//2.將JSON格式的字符串轉換Java對象
Student shxt = objectMapper.readValue(jsonStr, Student.class);
System.out.println("JSON格式字符串轉Student對象:"+shxt);
System.out.println("==============================================");
List<Student> studentList = new ArrayList<Student>();
studentList.add(student);
student = new Student();
student.setAccount("bajie");
student.setUserName("八戒");
studentList.add(student);
jsonStr = objectMapper.writeValueAsString(studentList);
System.out.println("StudentList轉JSON格式字符串:"+jsonStr);
JavaType t = objectMapper.getTypeFactory().constructParametricType(List.class, Student.class);
List<Student> shxtList = objectMapper.readValue(jsonStr, t);
System.out.println("JSON格式字符串轉shxtList集合:"+shxtList);
List<Map<String,Object>> mapList = objectMapper.readValue(jsonStr, List.class);
System.out.println("JSON格式字符串轉mapList集合:"+mapList);
}
}
簡單封裝工具類,在Java Web階段也許用到
public class JsonUtil {
/**
* 將對象轉換為json字符串
*
* @param obj
* @return
* @throws Exception
*/
public static String obj2string(Object obj) {
StringWriter sw = new StringWriter();
ObjectMapper mapper = new ObjectMapper();
try {
mapper.writeValue(sw, obj);
} catch (Exception e) {
}
return sw.toString();
}
/**
* 將字符串轉list對象
*
* @param <T>
* @param jsonStr
* @param cls
* @return
*/
public static <T> List<T> str2list(String jsonStr, Class<T> cls) {
ObjectMapper mapper = new ObjectMapper();
List<T> objList = null;
try {
JavaType t = mapper.getTypeFactory().constructParametricType(
List.class, cls);
objList = mapper.readValue(jsonStr, t);
} catch (Exception e) {
}
return objList;
}
/**
* 將字符串轉為對象
*
* @param <T>
* @param jsonStr
* @param cls
* @return
*/
public static <T> T str2obj(String jsonStr, Class<T> cls) {
ObjectMapper mapper = new ObjectMapper();
T obj = null;
try {
obj = mapper.readValue(jsonStr, cls);
} catch (Exception e) {
}
return obj;
}
/**
* 將字符串轉為Page對象
*
* @param <T>
* @param jsonStr
* @param cls
* @return
*/
public static <T> Page<T> str2page(String jsonStr, Class<T> cls) {
ObjectMapper mapper = new ObjectMapper();
Page<T> objList = null;
try {
JavaType t = mapper.getTypeFactory().constructParametricType(
Page.class, cls);
objList = mapper.readValue(jsonStr, t);
} catch (Exception e) {
}
return objList;
}
/**
* 將字符串轉為json節(jié)點
* @param jsonStr
* @return
*/
public static JsonNode str2node(String jsonStr) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readTree(jsonStr);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}