用到的bean
public class User {
private int id;
private String name;
private String password;
public User() {
}
public User(int id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
华丽的分割线:+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
json转换的Bean
package com.example.demo.text;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.bean.User;
/**
* @date 2020/1/6 10:11
*/
public class t1 {
public static void main(String[] args) {
User user = new User(1,"老王","老王");
// 转成Json
String json = JSON.toJSONString(user);
System.out.println(json);
final Object parse = JSON.parse(json);
System.out.println(parse);
// 变成JavaBean对象
final User user1 = JSONObject.parseObject(json, User.class);
System.out.println(user1);
}
}
华丽的分割线:——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
输出结果
{"id":1,"name":"老王","password":"老王"}
{"password":"老王","name":"老王","id":1}
User{id=1, name='老王', password='老王'}