欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

fastjson json串轉bean

江奕云2年前9瀏覽0評論

fastjson是一款高效的Java JSON庫,它可以將JSON串轉換為Java對象。下面我們就來介紹fastjson的Json串轉Bean的用法。

//JSON數據
String jsonString = "{\n" +
"\"name\": \"Tom\",\n" +
"\"age\": 20,\n" +
"\"address\": {\n" +
"\"province\": \"Hunan\",\n" +
"\"city\": \"Changsha\"\n" +
"}\n" +
"}";
//JavaBean,注意需要添加getter和setter方法
public class Person {
private String name;
private int age;
private Address address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
public class Address {
private String province;
private String city;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
// 將JSON串轉換為Java對象
Person person = JSON.parseObject(jsonString, Person.class);

使用fastjson,只需要調用JSON類的parseObject方法,并傳入需要轉換的JSON串和目標JavaBean的Class對象即可。需要注意的是,目標JavaBean中的屬性名必須和JSON串中的屬性名相同,否則fastjson會將值賦為null。