2014-10-19 60 views
0

我想使用JSON來包含與課程/課程相關的數據。這個想法是,有40個課程/課程,每個課程/課程包含50名學生,每個學生有100個作業。以下是我迄今爲止所瞭解到的。我如何修改它以保存上面列出的所有成績簿數據?JSON來保存課程,學生和作業數據

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(); 

       } 

      } 

    } 
+0

你想有這樣的[結構](http://pastebin.com/Gif6Y4eB)? – engineercoding 2014-10-19 18:35:58

+0

是的,但是必須有多個課程,每個課程有多個學生,每個學生有多個任務。 – Daron 2014-10-19 18:41:21

回答

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); 
} 
+0

我想感謝您的幫助。我將填寫缺失的代碼並仔細查看,以確定它是否可行或者我是否有任何問題。明天中午我會回到你身邊。我一直在研究這個問題,所以我希望這樣做。 – Daron 2014-10-19 20:55:02

+0

我把它看得很好。這無疑幫助我前進。 – Daron 2014-10-20 00:56:23