用于保存课程,学生和作业数据的JSON

时间:2014-10-19 18:08:25

标签: java json

我正在尝试使用JSON来包含与课程/课程相关的数据。这个想法是有40个课程/课程,每个课程/课程包含50个学生,每个学生有100个任务。以下是我到目前为止所发现的。如何修改它以保存所有成绩簿数据,如上所列?

public void x(){

        JSONObject courseJSONObject = new JSONObject();
        JSONArray courseJSONArray = new JSONArray();

        JSONObject studentJSONObject = new JSONObject();
        JSONArray studentJSONArray = new JSONArray();

        JSONObject assignmentJSONObject = new JSONObject();
        JSONArray assignmentJSONArray = new JSONArray();

        for(int i = 0; i < 40; i++){

            courseJSONObject.put("course name", course.getName());
            courseJSONObject.put("course teacher", course.getTeacher());

            courseJSONArray.put(courseJSONObject);

            courseJSONObject = new JSONObject();

            for(int j = 0; j < 50; j++){

                studentJSONObject.put("student name", course.student.getName());
                studentJSONObject.put("student id", course.student.getid());
                studentJSONObject.put("student final grade",  
                    course.student.getfinalgrade());

                studentJSONArray.put(studentJSONObject);

                studentJSONObject = new JSONObject();

                for(int k = 0; k < 100; k++){

                    assignmentJSONObject.put("assignment name", getAssignmentName());
                    assignmentJSONObject.put("category", getAssignmentCategory());
                    assignmentJSONObject.put("date", getAssignmentDate());
                    assignmentJSONObject.put("grade", 
                        course.student.getAssignmentGrade());

                    assignmentJSONArray.put(assignmentJSONArray);

                    assignmentJSONObject = new JSONObject();

                }

            }

    }

1 个答案:

答案 0 :(得分:1)

JSONArray courses = new JSONArray();
for(int c = 0; i < 40; c++) {
    JSONObject course = new JSONObject();
    // Add course details
    JSONArray students = new JSONArray();
    for(int s = 0; s < 50; s++) {
        JSONObject student = new JSONObject();
        // Add Student details
        JSONArray assignments = new JSONArray();
        for(int a = 0; a < 100; a++) {
            JSONObject assignment = new JSONObject();
            // Add assignment details
            assignments.put( assignment );
        }
        student.put( "assignments", assignments );
        students.put( student )
    }
    course.put( "students", students );
    courses.put( course );
}