<Dependency> <groupId>Net.sf.json-lib</groupId> <Artifactid>Json-lib</Artifactid> <version>2.4</version> <classifier>Jdk15</classifier> </Dependency>
Import Net.sf.json.JSONObject;
Public Static voidMain (string[] args) {//encapsulating JSON data using the map List//Example: The classroom has a desk, a blackboard, 30 students,//{"podium": "Desk a", "blackboard": "Blackboard A", "All students": [{"Student": "Xiao Ming"},{"student": "Xiao Hong"}]}Map<String,Object> Classroom =NewHashmap<string,object>(); Classroom.put ("Desk", "podium a"); Classroom.put ("Blackboard", "Blackboard a"); List<Map<String,Object>> students =NewArraylist<map<string,object>>(); Map<String,Object> Student1 =NewHashmap<string,object>(); Student1.put ("Student", "Xiaoming"); Students.add (STUDENT1); Map<String,Object> Student2 =NewHashmap<string,object>(); Student2.put ("Student", "Little Red"); Students.add (Student2); Classroom.put ("Students", students); System.out.println (Jsonobject.frommap (classroom). toString ()); //Console://{"Students": [{"Student": "Xiao Ming"},{"Student": "Little Red"}], "blackboard": "Blackboard A", "desk": "Desk a"}Jsonobject Jo=Jsonobject.frommap (classroom); System.out.println (Jo.get ("Desk")); //Console://podium aSystem.out.println (Jo.get ("blackboard")); //Console://Blackboard aiterator<jsonobject> student = Jo.getjsonarray ("Students"). iterator (); while(Student.hasnext ()) {System.out.println (Student.next (). Get ("Student")); //Console://Xiao Ming//Little Red } }
Encapsulating JSON data using the map List