0

I have data in a JSON like this :

{
"output_type": "json",
"round_trip": false,
"search_queries": {
    "from": "AMQ",
    "to": "CGK",
    "date": "2013-01-03",
    "ret_date": "",
    "adult": 1,
    "child": 0,
    "infant": 0
},
"go_det": {
    "dep_airport": {
        "airport_code": "AMQ",
        "international": "0",
        "trans_name_id": "7564",
        "business_name": "PATTIMURA",
        "business_name_trans_id": "5923",
        "business_id": "20349",
        "country_name": "Indonesia ",
        "city_name": "Ambon",
        "province_name": "Maluku",
        "location_name": "Ambon"
    },
    "arr_airport": {
        "airport_code": "CGK",
        "international": "1",
        "trans_name_id": "7574",
        "business_name": "Soekarno-Hatta",
        "business_name_trans_id": "5935",
        "business_id": "20361",
        "country_name": "Indonesia ",
        "city_name": "Jakarta Barat",
        "province_name": "DKI Jakarta",
        "location_name": "Jakarta"
    },
    "date": "2013-01-03",
    "formatted_date": "03 Januari 2013"
},
"diagnostic": {
    "status": 200,
    "elapsetime": "1.9305",
    "memoryusage": "12.14MB",
    "confirm": "success",
    "lang": "id",
    "currency": "IDR"
},
"departures": {
    "result": [
        {
            "flight_id": "605783",
            "airlines_name": "BATAVIA",
            "flight_number": "Y6-852",
            "price_value": "1566900.00",
            "timestamp": "2012-10-25 08:51:48",
            "price_adult": "1566900.00",
            "price_child": "0.00",
            "price_infant": "0.00",
            "simple_departure_time": "06:55",
            "simple_arrival_time": "08:10",
            "stop": "Langsung",
            "long_via": "",
            "duration": "3 j 15 m",
            "image": "https://www.master18.tiket.com/images/icon_batavia.jpg"
        },
        {
            "flight_id": "605786",
            "airlines_name": "LION",
            "flight_number": "JT-1791",
            "price_value": "1391000.00",
            "timestamp": "2012-10-25 08:51:42",
            "price_adult": "1391000.00",
            "price_child": "0.00",
            "price_infant": "0.00",
            "simple_departure_time": "08:00",
            "simple_arrival_time": "10:35",
            "stop": "1 Transit",
            "long_via": "",
            "duration": "4 j 35 m",
            "image": "https://www.master18.tiket.com/images/icon_lion.jpg"
           }
       ]
    },
 }

I have tried a move like here :

Sample 1

Sample 2

But have not found the results.

My code before like this :

var success = function(response) {
      for ( var i = 0; i < response.go_det.length; ++i ) {

      strKotaAwal   = response[i].go_det.dep_airport.airport_code;
      strKotaTujuan = response[i].go_det.arr_airport.airport_code;

  };

and i want to extract another data, ex :

[flight_id] => 605783
[airlines_name] => BATAVIA
[flight_number] => Y6-852
[price_value] => 1566900.00
[simple_departure_time] => 06:55
[simple_arrival_time] => 08:10
[duration] => 3 j 15 m
[image] => https://www.master18.tiket.com/images/icon_batavia.jpg
Community
  • 1
  • 1
Bertho Joris
  • 1,483
  • 5
  • 27
  • 60

2 Answers2

0

If you are using jQuery, you can use parseJSON method that takes a well-formed JSON string and returns the resulting JavaScript object.

0

Ill elaborate rkm's answer a bit:

As long as the response is a javascript object amd not a JSON string you can use the $.each() method to iterate over the result. Otherwise use the $.parseJSON() method mentioned above.

$.each(response.go_det, function(i, val){
    console.log(val);
});

Since your response contains both object and string properties you might want to separate them, and also use some nested loops, in case you want to reach properties on each object "further down":

$.each(response.go_det, function(i, val){
    if(typeof(val) === 'string'){
        console.log(val);
    } else if (typeof(val) === 'object'){
         $.each(val, function(i2, val2){
             console.log(val2); //etc...
         });
    }
});

So, for example, if you want each flight_id you could do something like this:

$.each(object.departures.result, function(i, val){
    console.log(val.flight_id);
});
Johan
  • 35,120
  • 54
  • 178
  • 293