I've been working on extracting data from a JSON response, but I'm running into an issue where I can't retrieve all the necessary values.
Below is the structure of the JSON response:
{
"status": "success",
"reservations": [
{
"id": "38177",
"subject": "subjectID",
"modifiedDate": "2017-05-16T12:46:17",
"startDate": "2017-05-30T08:00:00",
"endDate": "2017-05-30T22:00:00",
"resources": [
{
"id": "124",
"type": "room",
"code": "F407",
"parent": {
"id": "4",
"type": "building",
"code": "buildingF",
"name": "buildingName"
},
"name": " F407 (atk 34)"
}
],
"description": ""
},
{
// More reservation objects with similar structure
}
]
}
The task at hand involves extracting the room code and name for each subject as shown below:
"code": "F407"
"name": "F407 (atk 34)"
"code": "F411"
"name": "F411 (atk 34)"
"code": "F211"
"name": "F211 (room 50)"
"code": "F312"
"name": "F312 (room 48)"
I've attempted to achieve this using my own code, however, it seems to skip one of the room names inexplicably. My approach involves iterating through the JSON response with for loops and retrieving the code and name within resources
which are then added to an array:
var rooms = [];
for (var i = 0; i < json.reservations.length; i++) {
if (json.reservations[i].resources != null) {
for (var j = 0; j < json.reservations[i].resources.length; j++) {
var reservation = json.reservations[i];
var resource = json.reservations[i].resources[j];
if (resource.type === "room") {
if (rooms.indexOf("code")) {
rooms.push(resource.code + resource.name);
}
}
}
}
}
document.getElementById("pageOne").innerHTML = rooms.join("<br/>");
Upon execution, the output displays missing room names, specifically "name": "F411 (atk 34)"
.
Any insights on why this inconsistency might be occurring?