原文轉帖於:https://www.cnblogs.com/teach/p/5791029.htmlhtml
在開發過程當中,常常須要和別的系統交換數據,數據交換的格式有XML、JSON等,JSON做爲一個輕量級的數據格式比xml效率要高,XML須要不少的標籤,這無疑佔據了網絡流量,JSON在這方面則作的很好,下面先看下JSON的格式。前端
從上面的兩種格式能夠看出對象格式和數組對象格式惟一的不一樣則是在對象格式的基礎上加上了[],再來看具體的結構,能夠看出都是以鍵值對的形式出現的,中間以英文狀態下的逗號(,)分隔。
在前端和後端進行數據傳輸的時候這種格式也是很受歡迎的,後端返回json格式的字符串,前臺使用js中的JSON.parse()方法把JSON字符串解析爲json對象,而後進行遍歷,供前端使用。java
Jar包的版本號能夠忽略,目前使用過多種版本均沒發現什麼問題json
commons-beanutils-1.9.1.jar後端
commons-collections-3.2.2.jar數組
commons-lang-2.6.jar網絡
commons-logging-1.2.jar測試
ezmorph-1.0.6.jarthis
json-lib-2.4-jdk15.jar編碼
建立一個實體類用於測試
package com.org.demo; public class Students { private String stuName; private String stuAge; private String address; public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public String getStuAge() { return stuAge; } public void setStuAge(String stuAge) { this.stuAge = stuAge; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String toString() { return "Student [stuName=" + stuName + ", stuAge=" + stuAge + ", address=" + address + "]"; } }
編寫轉換方法
// Java對象轉JSON字符串 public static void javaToJsonStr() { Students students = new Students(); students.setStuName("張三"); students.setStuAge("28"); students.setAddress("廣州天河區"); // 一、使用JSONObject JSONObject strJson = JSONObject.fromObject(students); // 二、使用JSONArray JSONArray strArray = JSONArray.fromObject(students); System.out.println("Java對象轉JSON字符串"); System.out.println("JSON:" + strJson.toString()); System.out.println("Array:" + strArray.toString()); }
運行結果
Java對象轉JSON字符串 JSON:{"address":"廣州天河區","stuName":"張三","stuAge":"28"} Array:[{"address":"廣州天河區","stuName":"張三","stuAge":"28"}]
public static void jsonStrToJava() { // 定義兩種不一樣格式的字符串 String objectStr = "{\"stuName\":\"張三\",\"stuAge\":\"28\",\"address\":\"廣州天河區\"}"; String arrayStr = "[{\"stuName\":\"張三\",\"stuAge\":\"28\",\"address\":\"廣州天河區\"}]"; // 一、使用JSONObject JSONObject jsonObject = JSONObject.fromObject(objectStr); Students students = (Students) JSONObject.toBean(jsonObject, Students.class); // 二、使用JSONArray JSONArray jsonArray = JSONArray.fromObject(arrayStr); // 得到jsonArray的第一個元素 Object o = jsonArray.get(0); JSONObject jsonObject2 = JSONObject.fromObject(o); Students students2 = (Students) JSONObject.toBean(jsonObject2, Students.class); System.out.println("JSON字符串轉Java對象"); System.out.println("students:" + students); System.out.println("students2:" + students2); }
運行結果
JSON字符串轉Java對象 students:Student [stuName=張三, stuAge=28, address=廣州天河區] students2:Student [stuName=張三, stuAge=28, address=廣州天河區]
//List轉換爲Json字符串 public static void listToJson() { Students stu = new Students(); stu.setStuName("張三"); stu.setStuAge("28"); stu.setAddress("廣州天河區"); List<Students> lists = new ArrayList<Students>(); lists.add(stu); JSONArray listArray = JSONArray.fromObject(lists); System.out.println("listArray:" + listArray.toString()); }
運行結果
listArray:[{"address":"廣州天河區","stuName":"張三","stuAge":"28"}]
//Json字符串轉換爲List對象 public static void jsonToList() { String arrayStr = "[{\"stuName\":\"張三\",\"stuAge\":\"28\",\"address\":\"廣州天河區\"}]"; // 轉化爲list List<Students> list = (List<Students>) JSONArray.toList(JSONArray.fromObject(arrayStr), Students.class); for (Students svo : list) { System.out.println(svo); } // 轉化爲數組 Students[] stuArray = (Students[]) JSONArray.toArray(JSONArray.fromObject(arrayStr), Students.class); for (Students student : stuArray) { System.out.println(student); } }
運行結果
Student [stuName=張三, stuAge=28, address=廣州天河區] Student [stuName=張三, stuAge=28, address=廣州天河區]
//Map轉Json字符串 public static void mapToJson() { Students stu = new Students(); stu.setStuName("張三"); stu.setStuAge("28"); stu.setAddress("廣州天河區"); Map<String, Students> map = new HashMap<String, Students>(); map.put("first", stu); // 一、JSONObject JSONObject mapObject = JSONObject.fromObject(map); System.out.println("mapObject" + mapObject.toString()); // 二、JSONArray JSONArray mapArray = JSONArray.fromObject(map); System.out.println("mapArray:" + mapArray.toString()); }
運行結果
mapObject{"first":{"address":"廣州天河區","stuName":"張三","stuAge":"28"}} mapArray:[{"first":{"address":"廣州天河區","stuName":"張三","stuAge":"28"}}]
JSON字符串不能直接轉化爲map對象,要想取得map中的鍵對應的值須要別的方式
//Json字符串轉Map public static void jsonToMap() { String strObject = "{\"first\":{\"address\":\"廣州天河區\",\"stuAge\":\"28\",\"stuName\":\"張三\"}}"; // JSONObject JSONObject jsonObject = JSONObject.fromObject(strObject); Map<String, Object> map = new HashMap<String, Object>(); map.put("first", Students.class); // 使用了toBean方法,須要三個參數 MyBean my = (MyBean) JSONObject.toBean(jsonObject, MyBean.class, map); System.out.println(my.getFirst()); }
運行結果
Student [stuName=張三, stuAge=28, address=廣州天河區]
MyBean對象代碼
package com.org.demo; public class MyBean { private Students first; public Students getFirst() { return first; } public void setFirst(Students first) { this.first = first; } }
使用toBean()方法是傳入了三個參數,第一個是JSONObject對象,第二個是MyBean.class,第三個是一個Map對象。經過MyBean能夠知道此類中要有一個first的屬性,且其類型爲Students,要和map中的鍵和值類型對應,即,first對應鍵 first類型對應值的類型。 關於複雜的JSON串轉化爲java對象的內容,在下篇中會繼續說明。 本篇,主要說明了java中的bean、list、map和JSON的互轉。