Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

put(JSONObject) is undefined for the type JSONArray

Tags:

json

arrays

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

}

like image 298
Daron Avatar asked Dec 19 '25 19:12

Daron


1 Answers

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.

like image 140
Daron Avatar answered Dec 21 '25 10:12

Daron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!