2014-10-20 36 views
1

下面的代碼有兩個錯誤:一個在第三行到最後一行,一個在最後一行。這些行如下所示,並且都包含以下錯誤消息「方法put(JSONObject)對於JSONArray類型未定義。」這是什麼意思?如何解決?put(JSONObject)對於JSONArray類型是未定義的

studentJSONArray.put(studentJSONObject); //這是第三到最後一行

courseJSONArray.put(courseJSONObject); //這是最後一行

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.put(assignmentJSONObject); 
        } 
       studentJSONObject.put("assignments", assignmentJSONArray); 
       studentJSONArray.put(studentJSONObject); 
       } 
      courseJSONObject.put("students", studentJSONArray); 
      courseJSONArray.put(courseJSONObject); 

}

回答

1

事實證明,這兩行包含錯誤,因爲JSON數組使用「add」而不是「put」方法。對我來說JSON對象使用「put」方法而JSON數組使用「add」方法似乎並不一致。但我相信這是有原因的。

相關問題