How to loop through JSON object

This question already has an answer here:

  • How do I loop through or enumerate a JavaScript object? 29 answers

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

    I would use a simple recursive function to iterate through an object. It makes possible iterating through nested objects.

    // 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"
            }]
    };
    

    Define somewhere this function:

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

    So, now you're able to walk through sub elements by using:

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

    You also can pass entire "response" object to the function. It will has same effect. Additionally you'll get access to other elements like "status_message" etc.

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

    上一篇: 如何检查对象中的数组是否全部为空?

    下一篇: 如何循环浏览JSON对象