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