当前位置: 首页 > article >正文

实体类和 Map互相转换

使用 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博客


http://www.kler.cn/a/105850.html

相关文章:

  • 【四川乡镇界面】图层shp格式arcgis数据乡镇名称和编码2020年wgs84无偏移内容测评
  • 安卓(android)读取手机通讯录【Android移动开发基础案例教程(第2版)黑马程序员】
  • 初始化mysql报错cannot open shared object file: No such file or directory
  • 理解PLT表和GOT表
  • 29. C语言 可变参数详解
  • 2025 = 1^3 + 2^3 + 3^3 + 4^3 + 5^3 + 6^3 + 7^3 + 8^3 + 9^3
  • list列表前端分页功能已经提交list时容易犯错的问题回顾
  • 3D模型如何添加表面贴图?
  • selenium+python自动化安装驱动 碰到的问题
  • 安装python虚拟环境
  • 为什么进行压力测试? 有哪些方法?
  • 重入漏洞EtherStore
  • new Vue() 发生了什么?
  • 垃圾回收系统小程序
  • 一文知晓Linux文件权限
  • 面向对象设计——原型模式
  • 【Unity数据交互】JSON开山篇
  • 系列二、Spring Framework(Spring的优缺点是什么)
  • 基于STM32温湿度传感器采集报警系统设计
  • RPA厂商大比拼,哪家才更适合您?
  • word2vec两种优化方式的联系和区别
  • 如何选择专业的汽车托运平台
  • ‍ IT行业就业趋势:哪些方向更受青睐?
  • 深入理解数据结构(1)—用链表实现栈
  • RN读写json文件
  • 汽车托运是怎样收费