常用json與javabean互轉的方法實現
JSONObject 與 JSONArray區別
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){ //先通過構造函數初始化一個對象 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); } }
運行結果
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中數組類型數據取出轉成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());
輸出結果
到此這篇關於常用json與javabean互轉的方法實現的文章就介紹到這瞭,更多相關json與javabean互轉內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- None Found