使用Java構(gòu)造和解析Json數(shù)據(jù)的兩種方法(詳解一)
JSON(JavaScript Object Notation) 是一種輕量級的數(shù)據(jù)交換格式,采用完全獨立于語言的文本格式,是理想的數(shù)據(jù)交換格式。同時,JSON是 JavaScript 原生格式,這意味著在 JavaScript 中處理 JSON數(shù)據(jù)不須要任何特殊的 API 或工具包。
在www.json.org上公布了很多JAVA下的json構(gòu)造和解析工具,其中org.json和json-lib比較簡單,兩者使用上差不多但還是有些區(qū)別。下面首先介紹用json-lib構(gòu)造和解析Json數(shù)據(jù)的方法示例。
用org.son構(gòu)造和解析Json數(shù)據(jù)的方法詳解請參見我下一篇博文:使用Java構(gòu)造和解析Json數(shù)據(jù)的兩種方法(詳解二)
一、介紹
JSON-lib包是一個beans,collections,maps,java arrays 和XML和JSON互相轉(zhuǎn)換的包,主要就是用來解析Json數(shù)據(jù),在其官網(wǎng)http://www.json.org/上有詳細講解,有興趣的可以去研究。
二、下載jar依賴包:可以去這里下載
三、基本方法介紹
1. List集合轉(zhuǎn)換成json方法
List list = new ArrayList(); list.add( "first" ); list.add( "second" ); JSONArray jsonArray2 = JSONArray.fromObject( list );
2. Map集合轉(zhuǎn)換成json方法
Map map = new HashMap(); map.put("name", "json"); map.put("bool", Boolean.TRUE); map.put("int", new Integer(1)); map.put("arr", new String[] { "a", "b" }); map.put("func", "function(i){ return this.arr[i]; }"); JSONObject json = JSONObject.fromObject(map);
3. Bean轉(zhuǎn)換成json代碼
JSONObject jsonObject = JSONObject.fromObject(new JsonBean());
4. 數(shù)組轉(zhuǎn)換成json代碼
boolean[] boolArray = new boolean[] { true, false, true }; JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
5. 一般數(shù)據(jù)轉(zhuǎn)換成json代碼
JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']" );
6. beans轉(zhuǎn)換成json代碼
List list = new ArrayList(); JsonBean2 jb1 = new JsonBean2(); jb1.setCol(1); jb1.setRow(1); jb1.setValue("xx"); JsonBean2 jb2 = new JsonBean2(); jb2.setCol(2); jb2.setRow(2); jb2.setValue(""); list.add(jb1); list.add(jb2); JSONArray ja = JSONArray.fromObject(list);
四、演示示例
這里以基本的幾個常用方法進行測試
package com.json; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; /** * 使用json-lib構(gòu)造和解析Json數(shù)據(jù) * * @author Alexia * @date 2013/5/23 * */ public class JsonTest { /** * 構(gòu)造Json數(shù)據(jù) * * @return */ public static String BuildJson() { // JSON格式數(shù)據(jù)解析對象 JSONObject jo = new JSONObject(); // 下面構(gòu)造兩個map、一個list和一個Employee對象 Map<String, String> map1 = new HashMap<String, String>(); map1.put("name", "Alexia"); map1.put("sex", "female"); map1.put("age", "23"); Map<String, String> map2 = new HashMap<String, String>(); map2.put("name", "Edward"); map2.put("sex", "male"); map2.put("age", "24"); List<Map> list = new ArrayList<Map>(); list.add(map1); list.add(map2); Employee employee = new Employee(); employee.setName("wjl"); employee.setSex("female"); employee.setAge(24); // 將Map轉(zhuǎn)換為JSONArray數(shù)據(jù) JSONArray ja1 = JSONArray.fromObject(map1); // 將List轉(zhuǎn)換為JSONArray數(shù)據(jù) JSONArray ja2 = JSONArray.fromObject(list); // 將Bean轉(zhuǎn)換為JSONArray數(shù)據(jù) JSONArray ja3 = JSONArray.fromObject(employee); System.out.println("JSONArray對象數(shù)據(jù)格式:"); System.out.println(ja1.toString()); System.out.println(ja2.toString()); System.out.println(ja3.toString()); // 構(gòu)造Json數(shù)據(jù),包括一個map和一個Employee對象 jo.put("map", ja1); jo.put("employee", ja2); System.out.println("\n最終構(gòu)造的JSON數(shù)據(jù)格式:"); System.out.println(jo.toString()); return jo.toString(); } /** * 解析Json數(shù)據(jù) * * @param jsonString Json數(shù)據(jù)字符串 */ public static void ParseJson(String jsonString) { // 以employee為例解析,map類似 JSONObject jb = JSONObject.fromObject(jsonString); JSONArray ja = jb.getJSONArray("employee"); List<Employee> empList = new ArrayList<Employee>(); // 循環(huán)添加Employee對象(可能有多個) for (int i = 0; i < ja.size(); i++) { Employee employee = new Employee(); employee.setName(ja.getJSONObject(i).getString("name")); employee.setSex(ja.getJSONObject(i).getString("sex")); employee.setAge(ja.getJSONObject(i).getInt("age")); empList.add(employee); } System.out.println("\n將Json數(shù)據(jù)轉(zhuǎn)換為Employee對象:"); for (int i = 0; i < empList.size(); i++) { Employee emp = empList.get(i); System.out.println("name: " + emp.getName() + " sex: " + emp.getSex() + " age: " + emp.getAge()); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ParseJson(BuildJson()); } }
運行結(jié)果如下
五、與org.json比較
json-lib和org.json的使用幾乎是相同的,我總結(jié)出的區(qū)別有兩點:
1. org.json比json-lib要輕量得多,前者沒有依賴任何其他jar包,而后者要依賴ezmorph和commons的lang、logging、beanutils、collections等組件
2. json-lib在構(gòu)造bean和解析bean時比org.json要方便的多,json-lib可直接與bean互相轉(zhuǎn)換,而org.json不能直接與bean相互轉(zhuǎn)換而需要map作為中轉(zhuǎn),若將bean轉(zhuǎn)為json數(shù)據(jù),首先需要先將bean轉(zhuǎn)換為map再將map轉(zhuǎn)為json,比較麻煩。
總之,還是那句話—適合自己的才是最好的,大家要按需選取使用哪種方法進行解析。最后給大家介紹兩款解析Json數(shù)據(jù)的工具:一是在線工具JSON Edit(http://braincast.nl/samples/jsoneditor/);另一個是Eclipse的插件JSON Tree Analyzer,都很好用,推薦給大家使用!
以上所述是小編給大家介紹的使用Java構(gòu)造和解析Json數(shù)據(jù)的兩種方法(詳解一),希望對大家有所幫助!
相關(guān)文章
SpringBoot使用knife4j進行在線接口調(diào)試
這篇文章主要介紹了SpringBoot使用knife4j進行在線接口調(diào)試,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09SpringBoot利用隨機鹽值實現(xiàn)密碼的加密與驗證
這篇文章主要為大家詳細介紹了SpringBoot如何利用隨機鹽值實現(xiàn)密碼的加密與驗證,文中的示例代碼講解詳細,有需要的小伙伴可以參考下2024-02-02詳解SpringBoot2.0的@Cacheable(Redis)緩存失效時間解決方案
這篇文章主要介紹了詳解SpringBoot2.0的@Cacheable(Redis)緩存失效時間解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04SpringBoot整合Spring?Boot?Admin實現(xiàn)服務(wù)監(jiān)控的方法
這篇文章主要介紹了SpringBoot整合Spring?Boot?Admin實現(xiàn)服務(wù)監(jiān)控,內(nèi)容包括Server端服務(wù)開發(fā),Client端服務(wù)開發(fā)其中Spring Boot Admin還可以對其監(jiān)控的服務(wù)提供告警功能,如服務(wù)宕機時,可以及時以郵件方式通知運維人員,感興趣的朋友跟隨小編一起看看吧2022-03-03利用openoffice+jodconverter-code-3.0-bate4實現(xiàn)ppt轉(zhuǎn)圖片
這篇文章主要為大家詳細介紹了利用openoffice+jodconverter-code-3.0-bate4實現(xiàn)ppt轉(zhuǎn)圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07