实现思路
Student(对象) 转换成json字符串 解析成Map(对象)
Map(对象) 转换成json字符串 解析成Student(对象)
连个主要方法
JSON.toJSONString(对象);
JSON.parseObject(jsonString,Class);
Student student = new Student();
student.setId(1);
student.setStudentName("狗蛋");
student.setScore(new BigDecimal("20.213"));
student.setBirthday(new Date());
String studentJson = JSON.toJSONString(student);
System.out.println("studentJson:" + studentJson);
Map map = JSON.parseObject(studentJson, Map.class);
System.out.println("map:" + map);
String mapJson = JSON.toJSONString(map);
Student student1 = JSON.parseObject(mapJson, Student.class);
System.out.println(student1);
打印结果
studentJson:{"birthday":1572340893369,"id":1,"score":20.213,"studentName":"狗蛋"}
map:{birthday=1572340893369, score=20.213, studentName=狗蛋, id=1}
Student{id=1, studentName='狗蛋', score=20.213, birthday=Tue Oct 29 17:21:33 CST 2019}
注意如果对象中属性为date类型,转成map的时候变成了时间戳,此时需要考虑一些时间展示问题。