一个简单的JSONDEMO

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

⼀个简单的JSONDEMO
 ⽤到的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;
= 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) {
= 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 er;
/**
* @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='⽼王'}。

相关文档
最新文档