您好,登錄后才能下訂單哦!
這篇文章主要介紹“JSON和Javabean如何實現互轉”,在日常操作中,相信很多人在JSON和Javabean如何實現互轉問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。