JackJson工具Json字符串与List转换
主要使用以下2个API方法
1. public
2. public
代码示例:
ObjectMapper om = new ObjectMapper();
Map<String, Object> map = new HashMap<>();
map.put("name", "张三");
map.put("age", 20);
List<Book> bookList = new ArrayList<>();
for (int i = 1; i <= 3; i++) {
Book book = new Book();
book.setId(i + "");
book.setName("book" + i);
bookList.add(book);
map.put("bookList", bookList);
}
String s = om.writeValueAsString(map);
Map<String, Object> m = om.readValue(s, Map.class);
#第一种方法:TypeReference
TypeReference ref = new TypeReference<List<Book>>() {};
List<Book> list1 = om.readValue(om.writeValueAsString(m.get("bookList")), ref);
#第二种方法:CollectionType ,CollectionType是JavaType的子类
CollectionType type = om.getTypeFactory().constructCollectionType(List.class,Book.class);
List<Book> list2 = om.readValue(om.writeValueAsString(m.get("bookList")), type);