您好,登錄后才能下訂單哦!
這篇文章主要介紹“JSON和Javabean如何實現(xiàn)互轉(zhuǎn)”,在日常操作中,相信很多人在JSON和Javabean如何實現(xiàn)互轉(zhuǎn)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”JSON和Javabean如何實現(xiàn)互轉(zhuǎn)”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
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對象轉(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"));
}
}
方法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);
}
}
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"}]}";
//先轉(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));
}
}
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());
到此,關(guān)于“JSON和Javabean如何實現(xiàn)互轉(zhuǎn)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。