常用json與javabean互轉(zhuǎn)的方法實現(xiàn)
JSONObject 與 JSONArray區(qū)別
JSONObject:
{
"area": "武漢",
"name": "張三",
"age": 25
}
JSONArray:
[{
“area”: “武漢”,
“name”: “張三”,
“age”: 25
},
{
“area”: “深圳”,
“name”: “李四”,
“age”: 22
}]
通俗來講 JSONObject 是對象的json形式 JSONArry 是對象集合的JSON形式。
JSON 與javabean互轉(zhuǎn)
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;
}
}
1、javabean轉(zhuǎn)json
方法一:通過java對象轉(zhuǎn)成String再轉(zhuǎn)成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){
//先通過構(gòu)造函數(shù)初始化一個對象
User user=new User((long) 1,"admin","admin","張三",true,"123456");
//先將java對象轉(zhuǎn)為String類型
String jsonString= JSONObject.toJSONString(user);
//再將String類型轉(zhuǎn)為JSONObject
JSONObject jsonObject=JSONObject.parseObject(jsonString);
System.out.println(jsonObject);
//轉(zhuǎn)為JSONObject后就可以隨時根據(jù)鍵值獲取他的元素了
System.out.println(jsonObject.get("password"));
}
}
運(yùn)行結(jié)果

方法2:java對象直接轉(zhuǎn)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){
//先通過構(gòu)造函數(shù)初始化一個對象
User user=new User((long) 1,"admin","admin","張三",true,"123456");
JSONObject jsonObject= (JSONObject) JSONObject.toJSON(user);
System.out.println(jsonObject);
}
}
運(yùn)行結(jié)果

json字符串轉(zhuǎn)JSONObeject
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"));
}
}
運(yùn)行結(jié)果

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

4.JSON字符串轉(zhuǎn)JAVA對象
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());
輸出結(jié)果

到此這篇關(guān)于常用json與javabean互轉(zhuǎn)的方法實現(xiàn)的文章就介紹到這了,更多相關(guān)json與javabean互轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
微服務(wù)實戰(zhàn)之怎樣提升springboot服務(wù)吞吐量
這篇文章主要介紹了微服務(wù)實戰(zhàn)之怎樣提升springboot服務(wù)吞吐量方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
Spring Boot整合elasticsearch的詳細(xì)步驟
這篇文章主要介紹了Spring Boot整合elasticsearch的詳細(xì)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Spring復(fù)雜對象創(chuàng)建的方式小結(jié)
這篇文章主要介紹了Spring復(fù)雜對象創(chuàng)建的三種方式,現(xiàn)在使用Spring如何創(chuàng)建這種類型的對象?Spring中提供了三種方法來創(chuàng)建復(fù)雜對象,需要的朋友可以參考下2022-01-01

