7

This is my stringified JSON object:

{
    "bookingsList": [{
        "bookingID": 5,
        "destination": "BSO",
        "flightDate": "2015-12-07T00:00:00",
        "flightScheduleId": 113,
        "origin": "MNL",
        "passengerList": [{
            "availedInsurance": true,
            "availedMeal": false,
            "birthDate": null,
            "carryOnBaggage": 5.1,
            "checkInBaggage": 4.5,
            "columnId": 1,
            "firstname": "Peter",
            "gender": "M",
            "lastname": "North",
            "middlename": "West",
            "nationality": null,
            "rowId": "A"
        }]
    }, {
        "bookingID": 6,
        "destination": "BSO",
        "flightDate": "2015-12-07T00:00:00",
        "flightScheduleId": 113,
        "origin": "MNL",
        "passengerList": [{
            "availedInsurance": false,
            "availedMeal": false,
            "birthDate": null,
            "carryOnBaggage": 4.2,
            "checkInBaggage": 3.4,
            "columnId": 2,
            "firstname": "Mark Justin",
            "gender": "M",
            "lastname": "Jalandoni",
            "middlename": "Manzano",
            "nationality": null,
            "rowId": "A"
        }]
    }]
}

So how do I Iterate over the objects in the bookingsList? I will be placing some properties on a table.

EDIT: turns out that for in loop I was using wasn't iterating because I forgot to place a "var" prior to the object variable in the iterable.

2 Answers2

2

A simple for loop should suffice:

var jsonObj = {
    "bookingsList": [{
        "bookingID": 5,
        "destination": "BSO",
        "flightDate": "2015-12-07T00:00:00",
        "flightScheduleId": 113,
        "origin": "MNL",
        "passengerList": [{
            "availedInsurance": true,
            "availedMeal": false,
            "birthDate": null,
            "carryOnBaggage": 5.1,
            "checkInBaggage": 4.5,
            "columnId": 1,
            "firstname": "Peter",
            "gender": "M",
            "lastname": "North",
            "middlename": "West",
            "nationality": null,
            "rowId": "A"
        }]
    }, {
        "bookingID": 6,
        "destination": "BSO",
        "flightDate": "2015-12-07T00:00:00",
        "flightScheduleId": 113,
        "origin": "MNL",
        "passengerList": [{
            "availedInsurance": false,
            "availedMeal": false,
            "birthDate": null,
            "carryOnBaggage": 4.2,
            "checkInBaggage": 3.4,
            "columnId": 2,
            "firstname": "Mark Justin",
            "gender": "M",
            "lastname": "Jalandoni",
            "middlename": "Manzano",
            "nationality": null,
            "rowId": "A"
        }]
    }]
}

// For easy reference
var bookingsList = jsonObj.bookingsList;

for (i = 0; i < bookingsList.length; i++) {
    var booking = bookingsList[i]; // Grab booking data
    console.log(booking.bookingID); // Log booking data
}
Mr. Meeseeks
  • 1,841
  • 2
  • 21
  • 37
0
$.each(json.bookingsList, function(index, prop){
     console.log(prop);
});
350D
  • 11,135
  • 5
  • 36
  • 49
  • 2
    Code-only answers aren't really useful in helping the OP understand what is happening. Maybe give a short explanation of your answer. – wpercy Jan 26 '16 at 19:33