一. jar包
- commons-lang.jar
- commons-beanutils.jar
- commons-collections.jar
- commons-logging.jar
- ezmorph.jar
- json-lib-2.2.2-jdk15.jar
二. 代碼
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; public class Test { public static void main(String[] args) { new Test().test1(); new Test().test2(); new Test().test3(); new Test().test4(); new Test().test5(); new Test().test6(); new Test().test7(); } /** * JSON字符串轉JSONObject對象 */ public void test1() { String jsonStr = "{\"name\":\"ZhangSan\",\"sex\":\"boy\",\"age\":18}"; JSONObject jsonObj = JSONObject.fromObject(jsonStr); System.out.println(jsonObj.toString()); } /** * 簡單JSONObject對象轉java對象 */ public void test2() { JSONObject jsonObj = new JSONObject(); jsonObj.put("name", "LiSi"); jsonObj.put("sex", "girl"); jsonObj.put("age", 17); Student student = (Student) JSONObject.toBean(jsonObj, Student.class); System.out.println(student.getName() + " | " + student.getSex() + " | " + student.getAge()); } /** * 復雜JSONObject對象轉java對象 */ @SuppressWarnings("rawtypes") public void test3() { List<Student> students = new ArrayList<Student>(); students.add(new Student("ZhangSan", "boy", 18)); students.add(new Student("LiSi", "girl", 17)); BanJi banji = new BanJi(); banji.setBanJiName("日語二班"); banji.setStudents(students); JSONObject jsonObj = JSONObject.fromObject(banji); Map<String, Class> classMap = new HashMap<String, Class>(); classMap.put("students", Student.class); BanJi banji2 = (BanJi) JSONObject.toBean(jsonObj, BanJi.class, classMap); System.out.println(banji2.getStudents().get(0).getName()); // 驗證轉換是否成功 } /** * 簡單java集合對象轉JSONArray */ public void test4() { List<Student> students = new ArrayList<Student>(); students.add(new Student("ZhangSan", "boy", 18)); students.add(new Student("LiSi", "girl", 17)); JSONArray jsonArray = JSONArray.fromObject(students); System.out.println(jsonArray.toString()); } /** * JSONArray轉java集合對象 */ @SuppressWarnings({ "unchecked", "deprecation" }) public void test5() { JSONObject jsonObj1 = new JSONObject(); jsonObj1.put("name", "ZhangSan"); jsonObj1.put("sex", "boy"); jsonObj1.put("age", 18); JSONObject jsonObj2 = new JSONObject(); jsonObj2.put("name", "lisi"); jsonObj2.put("sex", "girl"); jsonObj2.put("age", 17); JSONArray jsonArray = new JSONArray(); jsonArray.add(0, jsonObj1); jsonArray.add(1, jsonObj2); List<Student> students3 = JSONArray.toList(jsonArray, Student.class); System.out.println(students3.get(0).getName()); System.out.println(students3.get(1).getName()); } /** * 復雜java集合對象轉JSONArray */ @SuppressWarnings({ "rawtypes", "unchecked", "deprecation" }) public void test6() { BanJi banji1 = new BanJi(); banji1.setBanJiName("日語一班"); List<Student> students1 = new ArrayList<Student>(); students1.add(new Student("ZhangSan", "boy", 18)); banji1.setStudents(students1); BanJi banji2 = new BanJi(); banji2.setBanJiName("日語二班"); List<Student> students2 = new ArrayList<Student>(); students1.add(new Student("LiSi", "girl", 17)); banji2.setStudents(students2); List<BanJi> banjiList = new ArrayList<BanJi>(); banjiList.add(banji1); banjiList.add(banji2); JSONArray jsonArray = JSONArray.fromObject(banjiList); Map<String, Class> classMap = new HashMap<String, Class>(); classMap.put("students", Student.class); List<BanJi> banjiList2 = JSONArray.toList(jsonArray, BanJi.class, classMap); System.out.println(banjiList2.get(0).getStudents().get(0).getName()); } /** * 拆分JSONArray為JSONObject */ public void test7() { JSONObject jsonObj1 = new JSONObject(); jsonObj1.put("name", "ZhangSan"); jsonObj1.put("sex", "boy"); jsonObj1.put("age", 18); JSONObject jsonObj2 = new JSONObject(); jsonObj2.put("name", "lisi"); jsonObj2.put("sex", "girl"); jsonObj2.put("age", 17); JSONArray jsonArray = new JSONArray(); jsonArray.add(0, jsonObj1); jsonArray.add(1, jsonObj2); JSONObject object = (JSONObject) jsonArray.get(0); System.out.println(object.toString()); } }
import java.util.List; /** * 班級類 */ public class BanJi { private String banJiName; // 班級名 private List<Student> students; // 學生 /** * 構造函數 */ public BanJi() { super(); } // getters/setters(略) }
/** * 學生類 */ public class Student { private String name; // 姓名 private String sex; // 性別 private int age; // 年齡 /** * 構造函數 */ public Student() { super(); } /** * 構造函數 */ public Student(String name, String sex, int age) { super(); this.name = name; this.sex = sex; this.age = age; } // getters/setters(略) }