0

我有这样的 JSON 数据:

{
"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"
           }
       ]
    },
 }

我尝试过像这里这样的举动:

样品 1

样品 2

但是还没有找到结果。

我之前的代码是这样的:

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;

  };

我想提取另一个数据,例如:

[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
4

2 回答 2

0

如果您使用的是 jQuery,则可以使用parseJSON采用格式良好的 JSON 字符串并返回结果 JavaScript 对象的方法。

于 2012-10-29T11:54:46.710 回答
0

我会详细说明 rkm 的答案:

只要response是 javascript 对象而不是 JSON 字符串,您就可以使用该$.each()方法迭代结果。否则使用上述$.parseJSON()方法。

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

由于您的响应包含对象和字符串属性,您可能希望将它们分开,并使用一些嵌套循环,以防您想要“进一步向下”访问每个对象的属性:

$.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...
         });
    }
});

因此,例如,如果您想要每个 flight_id,您可以执行以下操作:

$.each(object.departures.result, function(i, val){
    console.log(val.flight_id);
});
于 2012-10-29T12:10:08.007 回答