2016-12-09 165 views
1

通過Ajax我得到一個JSON對象循環,我想看看,雖然所有這些項目。這裏是我的ajax如何通過JSON對象

 document.getElementById('ACOptions').innerHTML = ""; 
    $.ajax({url: "SearchAvailableAircraft.php?ID=<?php echo $SimID; ?>&Code=<?php echo $SimCode; ?>&Search=" + S, success: function(result){ 
       /*alert(result);*/ 
       var MyAircraftList = result.AircraftList; 

而下面是我的回答

{"status":200,"status_message":"Valid Account","AircraftList":[{"ID":"1","FullTXT":"Boeing 777-200ER","TypeCode":"772","Manufacturer":"Boeing","Model":"777","Variant":"200ER","PaxCnt":"305","RangeNM":"5240","MinRwFT":"8000","Cost":"261500000","DeliveryDelay":"18"},{"ID":"2","FullTXT":"Airbus A320-200","TypeCode":"320","Manufacturer":"Airbus","Model":"A320","Variant":"200","PaxCnt":"186","RangeNM":"3300","MinRwFT":"2100","Cost":"98000000","DeliveryDelay":"9"}]}

我怎樣才能循環儘管這些知道我的目標是result.AircraftList

+0

不是因爲我有一個數組數組。 – Jason

+0

好,所以內部循環將從該答案。外循環將從這個答案:[在JavaScript中通過數組循環](http://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript) – styfle

+0

循環和做什麼? –

回答

0

可以使用.forEach陣列上迭代,然後用for in循環插件在性能迭代ide前面的循環。

var data = {"status":200,"status_message":"Valid Account","AircraftList":[{"ID":"1","FullTXT":"Boeing 777-200ER","TypeCode":"772","Manufacturer":"Boeing","Model":"777","Variant":"200ER","PaxCnt":"305","RangeNM":"5240","MinRwFT":"8000","Cost":"261500000","DeliveryDelay":"18"},{"ID":"2","FullTXT":"Airbus A320-200","TypeCode":"320","Manufacturer":"Airbus","Model":"A320","Variant":"200","PaxCnt":"186","RangeNM":"3300","MinRwFT":"2100","Cost":"98000000","DeliveryDelay":"9"}]} 
 

 
var MyAircraftList = data.AircraftList; 
 

 
MyAircraftList.forEach(function(aircraft, i) { 
 
    for (var key in aircraft) { 
 
    console.log(i, key, aircraft[key]); 
 
    } 
 
});

+0

我的代碼出現以下錯誤 - 未捕獲的TypeError:無法讀取未定義的屬性'forEach'(...) – Jason

+0

這意味着您調用'.forEach'的變量未定義,而不是預期的數組變量。 – styfle

+0

我該如何解決這個問題? – Jason

0

我會用一個簡單的遞歸函數通過對象進行迭代。它可以通過嵌套對象進行迭代。

// Your response 
var response = { 
    "status": 200, 
    "status_message": "Valid Account", 
    "AircraftList": [{ 
      "ID": "1", 
      "FullTXT": "Boeing 777-200ER", 
      "TypeCode": "772", 
      "Manufacturer": "Boeing", 
      "Model": "777", 
      "Variant": "200ER", 
      "PaxCnt": "305", 
      "RangeNM": "5240", 
      "MinRwFT": "8000", 
      "Cost": "261500000", 
      "DeliveryDelay": "18" 
     }, { 
      "ID": "2", 
      "FullTXT": "Airbus A320-200", 
      "TypeCode": "320", 
      "Manufacturer": "Airbus", 
      "Model": "A320", 
      "Variant": "200", 
      "PaxCnt": "186", 
      "RangeNM": "3300", 
      "MinRwFT": "2100", 
      "Cost": "98000000", 
      "DeliveryDelay": "9" 
     }] 
}; 

定義某處此功能:

function iterateRecursively(object, callbackFunction) { 

    // Walk through object 
    for (var keyName in object) { 

     // If the given parameter is an object, call the function over and 
     // over again, till you get a string or number 
     if (typeof object[keyName] === 'object') { 

      iterateRecursively(object[keyName], callbackFunction); 

     } else { 

      // Callback function to do something with results outside of main function 
      if (typeof callbackFunction === 'function') { 
       callbackFunction(keyName, object[keyName]); 
      } 

     } 
    } 
} 

所以,現在你可以通過子元素走路用:

iterateRecursively(response.AircraftList, function (key, value) { 

    // You could use "status" as condition. For example if you get some other status 
    // than "200", you could do something else (may be an error message). 
    // For example: 
    // if(key == 'status' && value != 200) doSomething(); 

    console.log(key + " = " + value); 
}); 

您也可以通過完整的 「響應」 對象到功能。它會有同樣的效果。此外,您還可以訪問其他元素,例如「status_message」等。

+0

這不起作用?爲什麼?我沒有得到一個有效的數組或對象? – Jason

+0

奇怪。你會得到什麼錯誤信息?什麼不正確?我稍微修改了一下函數,以便通過回調函數輕鬆訪問函數以外的功能。參見工作示例: https://jsfiddle.net/Lqs3o2ry/ – Hitmacher