1、介紹java
JSON-lib包是一個beans,collections,maps,java arrays 和XML和JSON互相轉換的包,主要就是用來解析Json數據json
2、下載jar依賴包:能夠去這裏下載數組
3、基本方法介紹post
1. List集合轉換成json方法測試
List list = new ArrayList(); list.add( "first" ); list.add( "second" ); JSONArray jsonArray2 = JSONArray.fromObject( list );
2. Map集合轉換成json方法ui
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轉換成json代碼this
JSONObject jsonObject = JSONObject.fromObject(new JsonBean());
4. 數組轉換成json代碼.net
boolean[] boolArray = new boolean[] { true, false, true }; JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
5. 通常數據轉換成json代碼code
JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']" );
6. beans轉換成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);
4、演示示例
這裏以基本的幾個經常使用方法進行測試
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構造和解析Json數據 * * @author Alexia * @date 2013/5/23 * */ public class JsonTest { /** * 構造Json數據 * * @return */ public static String BuildJson() { // JSON格式數據解析對象 JSONObject jo = new JSONObject(); // 下面構造兩個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轉換爲JSONArray數據 JSONArray ja1 = JSONArray.fromObject(map1); // 將List轉換爲JSONArray數據 JSONArray ja2 = JSONArray.fromObject(list); // 將Bean轉換爲JSONArray數據 JSONArray ja3 = JSONArray.fromObject(employee); System.out.println("JSONArray對象數據格式:"); System.out.println(ja1.toString()); System.out.println(ja2.toString()); System.out.println(ja3.toString()); // 構造Json數據,包括一個map和一個Employee對象 jo.put("map", ja1); jo.put("employee", ja2); System.out.println("\n最終構造的JSON數據格式:"); System.out.println(jo.toString()); return jo.toString(); } /** * 解析Json數據 * * @param jsonString Json數據字符串 */ public static void ParseJson(String jsonString) { // 以employee爲例解析,map相似 JSONObject jb = JSONObject.fromObject(jsonString); JSONArray ja = jb.getJSONArray("employee"); List<Employee> empList = new ArrayList<Employee>(); // 循環添加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數據轉換爲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()); } }
運行結果以下