大家好,欢迎来到IT知识分享网。
使用 JSON.parseObject(JSON.toJSONString(map), 类型.class)
// 实体类转换为Map Map map = JSON.parseObject(JSON.toJSONString(student), Map.class); // Map转实体类 Student student = JSON.parseObject(JSON.toJSONString(map), User.class);
例子:
实体类代码:
public class Student { private String name; private Integer age; private String gender; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } }
测试类代码:
Student student = new Student(); student.setName("张三"); student.setAge(10); student.setGender("男"); // 实体类转换为Map Map map = JSON.parseObject(JSON.toJSONString(student), Map.class); map.forEach((key,value)->{ System.out.println("key:" + key + ", value:" + value); }); // Map转实体类 Student student1 = JSON.parseObject(JSON.toJSONString(map), Student.class); System.out.println("姓名"+student1.getName()); System.out.println("年龄"+student1.getAge()); System.out.println("性别"+student1.getGender());
参考:实体类转 Map_实体类转map_Rock(洛克)的博客-CSDN博客
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/109964.html