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

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 に答える
0

jQueryを使用している場合parseJSONは、整形式のJSON文字列を受け取り、結果のJavaScriptオブジェクトを返すメソッドを使用できます。

于 2012-10-29T11:54:46.710 に答える