JSON数据与java对象转换
环境:
导入依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
样例:
public static void main(String[] args) {
//1.将java对象转换为jason
User user = new User();
user.setId(1);
user.setUsername("张三");
user.setPassword("123456");
String jsonString = JSON.toJSONString(user);//User对象转为Json对象
System.out.println(jsonString);
//将jason转换为java对象
User user1 = JSON.parseObject("{\"id\":1,\"password\":\"123456\",\"username\":\"张三\"}", User.class);//json对象转为jason对象
System.out.println(user1);
}
运行结果:
标签:转换,对象,JSON,User,java,jason,user From: https://www.cnblogs.com/JIANGzihao0222/p/17308208.html