对象与json之间的转换02

json数据与Java对象的转换
使用JSONObject类,包是json-lib;在springboot项目中使用需要添加jdk版本:
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
对象转json(其实就是字符串的意思)
String json = JSONObject.fromObject(user).toString();
对象转json
JSONObject jsonObject = JSONObject.fromObject(user);
User user = (User)JSONObject.toBean(jsonObject,User.class);

猜你喜欢

转载自www.cnblogs.com/lazyli/p/10824809.html