如何循环浏览JSON对象

这个问题在这里已经有了答案:

  • 如何遍历或枚举JavaScript对象? 29个答案

  • 您可以使用.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]);
      }
    });

    我会使用一个简单的递归函数遍历一个对象。 它可以通过嵌套对象进行迭代。

    // 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”等其他元素

    链接地址: http://www.djcxy.com/p/28841.html

    上一篇: How to loop through JSON object

    下一篇: ES6 Object.forOwn loop over object keys functionality