fasterxml jackson的使用--从pojo转成json字符串和json解析回pojo
程序员文章站
2024-02-04 11:09:40
...
1.从BookVo转成json
BookVo bookVo=new BookVo("sb", 1, "bigsb", "xx.jpg", "this is a sb", 48, new Date());
//创建对象映射器
ObjectMapper om=new ObjectMapper();
//读取BookVo对象并返回字符串
String book=om.writeValueAsString(bookVo);
System.out.println(book);
2.从json转回BookVo
ObjectMapper om=new ObjectMapper();
//使用readValue解析json,注意要指定json要转回的类型
BookVo bookVo =om.readValue("{\"name\":\"sb\",\"tid\":1,\"author\":\"bigsb\",\"photo\":\"xx.jpg\",\"bookinfo\":\"this is a sb\",\"price\":48.0,\"publicactiondate\":\"2018-01-18\"}\r\n" +
"", BookVo.class);
System.out.println(bookVo.toString());
注:
1.pojo(Plain Old Java Object)普通java对象
2.fasterxml jackson是json第二版,即json2.0
4.jsonj介绍http://www.json.org/json-zh.html