json與javabean怎么實現互轉?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
JSONObject:
{
"area": "武漢",
"name": "張三",
"age": 25
}JSONArray:
[{
“area”: “武漢”,
“name”: “張三”,
“age”: 25
},
{
“area”: “深圳”,
“name”: “李四”,
“age”: 22
}]通俗來講 JSONObject 是對象的json形式 JSONArry 是對象集合的JSON形式。
JSON用阿里的fastjson 包
用例java對象
public class User {
protected Long id;
protected String account;
protected String password;
protected String name;
protected boolean gender;
protected String telephone;
@Override
public String toString() {
return "User{" +
"id=" + id +
", account='" + account + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
", gender=" + gender +
", telephone='" + telephone + '\'' +
'}';
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}方法一:通過java對象轉成String再轉成JSONObject
package com.handoop.gms.utils;
import com.alibaba.fastjson.JSONObject;
import com.handoop.gms.domain.User;
public class TestMain {
public static void main(String []args){
//先通過構造函數初始化一個對象
User user=new User((long) 1,"admin","admin","張三",true,"123456");
//先將java對象轉為String類型
String jsonString= JSONObject.toJSONString(user);
//再將String類型轉為JSONObject
JSONObject jsonObject=JSONObject.parseObject(jsonString);
System.out.println(jsonObject);
//轉為JSONObject后就可以隨時根據鍵值獲取他的元素了
System.out.println(jsonObject.get("password"));
}
}運行結果

方法2:java對象直接轉json
package com.handoop.gms.utils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.handoop.gms.domain.User;
public class TestMain {
public static void main(String []args){
//先通過構造函數初始化一個對象
User user=new User((long) 1,"admin","admin","張三",true,"123456");
JSONObject jsonObject= (JSONObject) JSONObject.toJSON(user);
System.out.println(jsonObject);
}
}運行結果

public class TestMain {
public static void main(String []args){
String str="{\"password\":\"admin\",\"gender\":true,\"name\":\"張三\",\"telephone\":\"123456\",\"id\":1,\"account\":\"admin\"}";
JSONObject jsonObject=JSONObject.parseObject(str);
System.out.println("account: "+jsonObject.get("account")+"---"+"paasword: "+jsonObject.get("password"));
}
}運行結果

public class TestMain {
public static void main(String []args){
String str="{\"data\":[{\"password\":\"admin\",\"gender\":true,\"name\":\"張三\",\"telephone\":\"123456\",\"id\":1,\"account\":\"admin\"}]}";
//先轉成JSONObject
JSONObject jsonObject=JSONObject.parseObject(str);
//再將JSONObject中數組類型數據取出轉成JSONArray
JSONArray jsonArray=jsonObject.getJSONArray("data");
System.out.println(jsonArray.get(0));
}
}運行結果

String str="{\"password\":\"admin\",\"gender\":true,\"name\":\"張三\",\"telephone\":\"123456\",\"id\":1,\"account\":\"admin\"}";
// 前面是JSON字符串 后面是java對象類型
User user=JSONObject.parseObject(str,User.class);
System.out.println("account: "+user.getAccount()+"---"+"paasword: "+user.getPassword());輸出結果

關于json與javabean怎么實現互轉問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。