002.JDK方式——JSONObject、JSONArray
package com.jkd.entity; public class Computer { public String cpu; public String disk; public String getCpu() { return cpu; } public void setCpu(String cpu) { this.cpu = cpu; } public String getDisk() { return disk; } public void setDisk(String disk) { this.disk = disk; } @Override public String toString() { return "Computer{" + "cpu='" + cpu + '\'' + ", disk='" + disk + '\'' + '}'; } }
package com.jkd.entity; import java.util.ArrayList; public class School { public String name; public Computer computer; public ArrayList<Student> students; public String getName() { return name; } public void setName(String name) { this.name = name; } public Computer getComputer() { return computer; } public void setComputer(Computer computer) { this.computer = computer; } public ArrayList<Student> getStudents() { return students; } public void setStudents(ArrayList<Student> students) { this.students = students; } @Override public String toString() { return "School{" + "name='" + name + '\'' + ", computer=" + computer + ", students=" + students + '}'; } }
package com.jkd.entity; public class Student { public String name; public int age; public String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + '}'; } }
package com.jdk.json; import com.jkd.entity.Computer; import com.jkd.entity.School; import com.jkd.entity.Student; import org.json.JSONArray; import org.json.JSONObject; import java.util.ArrayList; public class JsonA { public static String jsonString = "{\n" + " \"name\": \"teacher\",\n" + " \"computer\": {\n" + " \"CPU\": \"intel7\",\n" + " \"disk\": \"512G\"\n" + " },\n" + " \"students\": [\n" + " {\n" + " \"name\": \"张三\",\n" + " \"age\": 18,\n" + " \"sex\": \"男\"\n" + " },\n" + " {\n" + " \"name\": \"李四\",\n" + " \"age\": 19,\n" + " \"sex\": \"男\"\n" + " }\n" + " ]\n" + "}\n"; public static void main(String[] args) { // 将Json转换为Java对象 School school=new School(); /** * 1.导入包import org.json.JSONObject */ JSONObject jsonObject = new JSONObject(jsonString); String name = (String) jsonObject.get("name"); school.setName(name); JSONObject computerJsonObject = jsonObject.getJSONObject("computer"); Computer computer=new Computer(); String cpu = (String) computerJsonObject.get("CPU"); String disk = (String) computerJsonObject.get("disk"); computer.setCpu(cpu); computer.setDisk(disk); school.setComputer(computer); ArrayList<Student> studentsList=new ArrayList<>(); JSONArray studentsJSONArray = jsonObject.getJSONArray("students"); for (int i=0;i<studentsJSONArray.length();i++) { JSONObject jsonObject1 = studentsJSONArray.getJSONObject(i); String name1 = (String) jsonObject1.get("name"); int age = (int) jsonObject1.get("age"); String sex = (String) jsonObject1.get("sex"); Student student=new Student(); student.setName(name1); student.setAge(age); student.setSex(sex); studentsList.add(student); } school.setStudents(studentsList); System.out.println(school); System.out.println("--------------------"); //java转json String s = jsonObject.toString(); System.out.println(s); } }