jackson的使用
@Test
public void test2(){
ObjectMapper objectMapper = new ObjectMapper();
People peo = new People(2,"zhangsan");
try {
//1.jackson对对象的操作
//吧数据转换成字符串
String s = objectMapper.writeValueAsString(peo);
System.out.println(s);
//把字符串转换成对象
People peo1 = objectMapper.readValue(s, People.class);
System.out.println(peo1);
List<People> list1 = new ArrayList<People>();
list1.add(peo);
//2.jackson对list的操作
System.out.println("----------------------");
//把List转换成的字符串
String s1 = objectMapper.writeValueAsString(list1);
System.out.println(s1);
//把字符串转换成List
List<People> result1 = objectMapper.readValue(s1, new TypeReference<List<People>>() {});
System.out.println(result1);
//3.jackson对map的操作
System.out.println("----------------------");
Map<String,People> map = new HashMap<String, People>();
map.put("peo",peo);
String s2 = objectMapper.writeValueAsString(map);
System.out.println(s2);
Map<String,People> result = objectMapper.readValue(s2, new TypeReference<Map<String,People>>() {});
System.out.println(result.get("peo"));
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
本人是个刚入职的小菜鸡,写下来只是一些随笔,用来自己回顾,很多东西不一定正确,只是我当下自己的理解,请各位大神,有错误的地方可以指出来哈。

浙公网安备 33010602011771号