慢JSON代码,三重循环

时间:2014-10-20 18:53:18

标签: java json

有没有人对如何让JSON更快地运行有任何想法?其中包含包含多个课程的成绩簿的信息,每个课程包含多个学生,每个学生包含多个作业。我知道使用三重循环可能会减慢速度。但这需要花费半个多小时来执行。

JSONArray courseJSONArray = new JSONArray();
        for(int c = 0; c < 40; c++) {
            JSONObject courseJSONObject = new JSONObject();
            courseJSONObject.put("course name", course.getName());
            courseJSONObject.put("course teacher", course.getTeacher());
            JSONArray studentJSONArray = new JSONArray();
            for(int s = 0; s < 50; s++) {
                JSONObject studentJSONObject = new JSONObject();
                studentJSONObject.put("student name", course.student.getName());
                studentJSONObject.put("student id", course.student.getid());
                studentJSONObject.put("student final grade", course.student.getfinalgrade());
                JSONArray assignmentJSONArray = new JSONArray();
                for(int a = 0; a < 100; a++) {
                    JSONObject assignmentJSONObject = new JSONObject();
                    assignmentJSONObject.put("assignment name", getAssignmentName());
                    assignmentJSONObject.put("category", getAssignmentCategory());
                    assignmentJSONObject.put("date", getAssignmentDate());
                    assignmentJSONObject.put("grade", course.student.getAssignmentGrade());
                    assignmentJSONArray.add( assignmentJSONObject );
                    }
                studentJSONObject.put( "assignments", assignmentJSONArray );
                studentJSONArray.add( studentJSONObject );
                }
            courseJSONObject.put( "students", studentJSONArray );
            courseJSONArray.add( courseJSONObject );
            }

0 个答案:

没有答案