2012-10-29 81 views
0

我有數據在這樣的JSON:多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" 
      } 
     ] 
    }, 
} 

我已經試過喜歡這裏一招:

Sample 1

Sample 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 
+0

什麼碼你嘗試過這麼遠嗎? –

+0

我上面寫... 變種成功=函數(響應){ 爲(VAR I = 0; I

+1

不應該有你的json對象中的最後一個逗號「},」應該是「}」 –

回答

0

生病精心RKM的回答有點:

只要response是一個JavaScript對象AMD不是你可以使用$.each()方法來遍歷結果的JSON字符串。否則使用上面提到的$.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); 
}); 
+0

好的johan,我會試試.. :) –

0

如果你使用jQuery,您可以使用parseJSON方法,需要一個良好形成JSON字符串並返回結果JavaScript對象。

+0

在他的問題中,他的JSON輸出對我來說已經看起來像JSON。沒有? –