import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class JSONTest { public static void main(String[] args) { JSONObject container1 = new JSONObject(); container1.put("ClassName", "高三一班"); System.out.println(container1.toString()); JSONArray className = new JSONArray(); className.add("高三一班"); container1.put("className", className); System.out.println(container1.toString()); JSONObject classInfo = new JSONObject(); classInfo.put("stuCount", 50); classInfo.put("leader", "rah"); container1.put("classInfo", classInfo); System.out.println(container1); JSONObject ClassInfo = new JSONObject(); JSONArray stuCount = new JSONArray(); stuCount.add(50); JSONArray leader = new JSONArray(); leader.add("rah"); ClassInfo.put("stuCount", stuCount); ClassInfo.put("leader", leader); container1.put("ClassInfo", ClassInfo); System.out.println(container1); JSONArray students = new JSONArray(); JSONObject studentOne = new JSONObject(); studentOne.put("name", "张麻子"); studentOne.put("sex", "男"); studentOne.put("age", 12); studentOne.put("hobby", "java develop"); JSONObject studentTwo = new JSONObject(); studentTwo.put("name", "王瘸子"); studentTwo.put("sex", "男"); studentTwo.put("age", 13); studentTwo.put("hobby", "C/C++ develop"); students.add(studentOne); students.add(studentTwo); container1.put("students", students); System.out.println(container1); JSONArray Students = new JSONArray(); JSONObject StudnetOne = new JSONObject(); JSONArray name1 = new JSONArray(); name1.add("张麻子"); JSONArray sex1 = new JSONArray(); sex1.add("男"); JSONArray age1= new JSONArray(); age1.add("12"); JSONArray hobby1 = new JSONArray(); hobby1.add("java develop"); StudnetOne.put("name", name1); StudnetOne.put("sex", sex1); StudnetOne.put("age", age1); StudnetOne.put("hobby", hobby1); JSONObject StudnetTwo = new JSONObject(); JSONArray name2 = new JSONArray(); name2.add("王瘸子"); JSONArray sex2 = new JSONArray(); sex2.add("男"); JSONArray age2= new JSONArray(); age2.add("13"); JSONArray hobby2 = new JSONArray(); hobby2.add("C/C++ develop"); StudnetTwo.put("name", name2); StudnetTwo.put("sex", sex2); StudnetTwo.put("age", age2); StudnetTwo.put("hobby", hobby2); Students.add(StudnetOne); Students.add(StudnetTwo); container1.put("Students", Students); System.out.println(container1); JSONArray teachers = new JSONArray(); teachers.add(0,"王老师"); teachers.add(1,"李老师 "); container1.put("teachers", teachers); System.out.println(container1); JSONArray Teachers = new JSONArray(); JSONObject teacher1 = new JSONObject(); teacher1.put("name", "小梅"); teacher1.put("introduce","他是一个好老师"); JSONObject teacher2 = new JSONObject(); teacher2.put("name", "小李"); teacher2.put("introduce","他是一个合格的老师"); Teachers.add(0,teacher1); Teachers.add(1,teacher2); container1.put("Teachers", Teachers); System.out.println(container1); } } |