场景说明
最近在写一个服务,经常用到调别人接口得到json,然后需要转换为 指定bean,记录一下常用工具类
引入依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> </dependency>
示例
bean
@Data public class User { private Integer id; private String name; private List<String> books; }
转换
@Test public void testjson() { User user = new User(); user.setId(1001); user.setName("金庸"); ArrayList<String> books = new ArrayList<>(); books.add("飞"); books.add("雪"); books.add("连"); books.add("天"); books.add("射"); books.add("白"); books.add("🦌"); user.setBooks(books); //使用 fastjson bean2json String jsonStr = JSON.toJSONString(user); System.out.println(jsonStr); //使用 fastjson json2bean User user1 = JSONObject.parseObject(jsonStr, User.class); System.out.println(user1); //使用 hutool bean2json String jsonStr1 = JSONUtil.toJsonStr(user); System.out.println(JSONObject.parseObject(jsonStr1, User.class)); //使用 hutool json2bean System.out.println(JSONUtil.toBean(jsonStr1, User.class)); }
执行效果:
{"books":["飞","雪","连","天","射","白","🦌"],"id":1001,"name":"金庸"} User(id=1001, name=金庸, books=[飞, 雪, 连, 天, 射, 白, 🦌]) User(id=1001, name=金庸, books=[飞, 雪, 连, 天, 射, 白, 🦌]) User(id=1001, name=金庸, books=[飞, 雪, 连, 天, 射, 白, 🦌])
JSON数组
来一个 jsonarray
@Test public void testjsonArray() { ArrayList<User> users = new ArrayList<>(); User jiny = new User(); jiny.setId(1001); jiny.setName("金庸"); ArrayList<String> books = new ArrayList<>(); books.add("飞"); books.add("雪"); books.add("连"); books.add("天"); books.add("射"); books.add("白"); books.add("🦌"); jiny.setBooks(books); User gul = new User(); gul.setId(1002); gul.setName("古龙"); ArrayList<String> bookgl = new ArrayList<>(); bookgl.add("绝"); bookgl.add("萧"); bookgl.add("流"); bookgl.add("天"); bookgl.add("武"); bookgl.add("大"); bookgl.add("小"); gul.setBooks(bookgl); users.add(jiny); users.add(gul); //hu tool 工具包 String jsonStr = JSONUtil.toJsonStr(users); System.out.println(jsonStr); List<User> list = JSONUtil.toList(jsonStr, User.class); System.out.println(users); System.out.println(list); System.out.println("--------------"); //fastjson 工具包 String jsonString = JSON.toJSONString(users); System.out.println(jsonString); JSONArray jsonArray = JSONArray.parseArray(jsonString); List<User> javaList = jsonArray.toJavaList(User.class); System.out.println(javaList); }
执行效果
[{"books":["飞","雪","连","天","射","白","🦌"],"name":"金庸","id":1001},{"books":["绝","萧","流","天","武","大","小"],"name":"古龙","id":1002}] [User(id=1001, name=金庸, books=[飞, 雪, 连, 天, 射, 白, 🦌]), User(id=1002, name=古龙, books=[绝, 萧, 流, 天, 武, 大, 小])] [User(id=1001, name=金庸, books=[飞, 雪, 连, 天, 射, 白, 🦌]), User(id=1002, name=古龙, books=[绝, 萧, 流, 天, 武, 大, 小])] -------------- [{"books":["飞","雪","连","天","射","白","🦌"],"id":1001,"name":"金庸"},{"books":["绝","萧","流","天","武","大","小"],"id":1002,"name":"古龙"}] [User(id=1001, name=金庸, books=["飞","雪","连","天","射","白","🦌"]), User(id=1002, name=古龙, books=["绝","萧","流","天","武","大","小"])]
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。