JSON因?yàn)槠鋸?qiáng)大特性的原因,是現(xiàn)階段web開(kāi)發(fā)中前后端交互的最主要的數(shù)據(jù)格式。后端開(kāi)發(fā)中經(jīng)常使用到Javabean,因此時(shí)常需要將JSON和Javabean進(jìn)行互相轉(zhuǎn)換。本文將為您介紹幾種關(guān)于如何應(yīng)用 Java 語(yǔ)言來(lái)實(shí)現(xiàn)轉(zhuǎn)換JSON和Javabean的常用方法。
JSONObject 與 JSONArray區(qū)別
JSONObject:
{
"area": "武漢",
"name": "張三",
"age": 25
}
JSONArray:
[{
“area”: “武漢”,
“name”: “張三”,
“age”: 25
},
{
“area”: “深圳”,
“name”: “李四”,
“age”: 22
}]
通俗來(lái)講 JSONObject 是對(duì)象的json形式 JSONArry 是對(duì)象集合的JSON形式。
JSON 與javabean互轉(zhuǎn)
JSON用阿里的fastjson 包
用例java對(duì)象
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
方法一:通過(guò)java對(duì)象轉(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){
//先通過(guò)構(gòu)造函數(shù)初始化一個(gè)對(duì)象
User user=new User((long) 1,"admin","admin","張三",true,"123456");
//先將java對(duì)象轉(zhuǎn)為String類(lèi)型
String jsonString= JSONObject.toJSONString(user);
//再將String類(lèi)型轉(zhuǎn)為JSONObject
JSONObject jsonObject=JSONObject.parseObject(jsonString);
System.out.println(jsonObject);
//轉(zhuǎn)為JSONObject后就可以隨時(shí)根據(jù)鍵值獲取他的元素了
System.out.println(jsonObject.get("password"));
}
}
運(yùn)行結(jié)果
方法2:java對(duì)象直接轉(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){
//先通過(guò)構(gòu)造函數(shù)初始化一個(gè)對(duì)象
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ù)組類(lèi)型數(shù)據(jù)取出轉(zhuǎn)成JSONArray
JSONArray jsonArray=jsonObject.getJSONArray("data");
System.out.println(jsonArray.get(0));
}
}
運(yùn)行結(jié)果
4.JSON字符串轉(zhuǎn)JAVA對(duì)象
String str="{"password":"admin","gender":true,"name":"張三","telephone":"123456","id":1,"account":"admin"}";
// 前面是JSON字符串 后面是java對(duì)象類(lèi)型
User user=JSONObject.parseObject(str,User.class);
System.out.println("account: "+user.getAccount()+"---"+"paasword: "+user.getPassword());
輸出結(jié)果
以上就是使用 Java代碼實(shí)現(xiàn)JSON和Javabean互轉(zhuǎn)的幾種常用方法的全部?jī)?nèi)容 ,想要了解更多相關(guān) Java 的內(nèi)容請(qǐng)搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章!