一、Gson
谷歌的JSON
- 将对象转换为JSON字符串
Book book = BookDao.find();
String jsonBood = new Gson().toJson(book);
- 将字符串转换为对象
String json = "{\"id\":1, \"name\":\"名字\",\"info\": 198.0}";
Book book = new Gson().fromJson(json, Book.class);
二、Fastjson
阿里的JSON工具包
- 将对象转换为JSON字符串
Book book = BookDao.find();
String jsonBood = JSON.toJSONString(book);
- 将字符串转换为对象
String json = "{\"id\":1, \"name\":\"名字\",\"info\": 198.0}";
Book book = JSON.parseObject(json, Book.class);