The code below has two errors...one in the 3rd to last line and one in the last line. The lines are as follows and both contain the following error message "The method put(JSONObject) is undefined for the type JSONArray." What does this mean and how can it be fixed?
studentJSONArray.put( studentJSONObject );//this is the 3rd to last line
courseJSONArray.put( courseJSONObject );//this is the last line
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 );
}
As it turns out, the two lines contained errors because JSON arrays use the "add" rather than the "put" method. It seems inconsistent to me for the JSON object to use the "put" method while the JSON array uses the "add" method. But I'm sure that there's some reason for that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With