如何循环JSON对象

时间:2016-12-09 21:46:46

标签: javascript json

通过AJAX我得到了一个JSON对象,我想看看所有这些项目..这里是我的ajax

        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;

以下是我的回复

  

{&#34; status&#34;:200,&#34; status_message&#34;:&#34;有效帐户&#34;,&#34; AircraftList&#34;:[{&#34; ID& #34;:&#34; 1&#34;,&#34; FullTXT&#34;:&#34;波音777-200ER&#34;,&#34; TypeCode&#34;:&#34; 772&#34 ;,&#34;制造商&#34;:&#34;波音&#34;&#34;型号&#34;:&#34; 777&#34;&#34;变形例&#34;:&#34 ; 200ER&#34;&#34; PaxCnt&#34;:&#34; 305&#34;&#34; RangeNM&#34;:&#34; 5240&#34;&#34; MinRwFT&#34; :&#34; 8000&#34;&#34;成本&#34;:&#34; 261500000&#34;&#34; DeliveryDelay&#34;:&#34; 18&#34;},{&# 34; ID&#34;:&#34; 2&#34;,&#34; FullTXT&#34;:&#34;空中客车A320-200&#34;,&#34; TypeCode&#34;:&#34; 320&#34;&#34;制造商&#34;:&#34;空中客车公司&#34;&#34;型号&#34;:&#34; A320&#34;&#34;变形例&#34 ;: &#34; 200&#34;&#34; PaxCnt&#34;:&#34; 186&#34;&#34; RangeNM&#34;:&#34; 3300&#34;&#34; MinRwFT& #34;:&#34; 2100&#34;&#34;成本&#34;:&#34; 98000000&#34;&#34; DeliveryDelay&#34;:&#34; 9&#34;}] }

我怎么能循环这些知道我的对象是result.AircraftList

2 个答案:

答案 0 :(得分:0)

您可以使用.forEach迭代数组,然后使用前一循环内的for in循环遍历属性。

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]);
  }
});

答案 1 :(得分: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);
});

您也可以通过整个&#34;响应&#34;对象的功能。它会产生同样的效果。此外,您还可以访问其他元素,例如&#34; status_message&#34;等。