迭代JSON AJAX响应

时间:2016-04-08 04:13:48

标签: javascript jquery json ajax

我需要查看每个酒店密钥并进入房价以检索一些信息,我怎样才能在JavaScript中使用这个JSON获取它们的值:

[
    {
        "auditData": {
            "processTime": "1545",
            "timestamp": "2016-04-08 04:33:17.145",
            "requestHost": "66.226.74.194",
            "serverId": "sa37AUX3ROLBLIS.env",
            "environment": "[int]",
            "release": "2cb1bad878d2195c9b508e2007ef96616007dacb",
            "internal": "bz66k2etuxrt5q5h2zyf3jf6|MX|03|HA|1|3|0|1|3|N|||||||||1"
        },
        "hotel": {
            "checkIn": "2016-04-11",
            "checkOut": "2016-04-12",
            "code": 87399,
            "name": "Premier",
            "categoryCode": "3EST",
            "categoryName": "3 ESTRELLAS",
            "destinationCode": "MDF",
            "destinationName": "Ciudad de Mexico",
            "zoneCode": 10,
            "zoneName": "Downtown",
            "latitude": "19.431353",
            "longitude": "-99.156457",
            "rooms": [
                {
                    "code": "DBL.2D",
                    "name": "DOBLE DOS CAMAS DOBLES",
                    "rates": [
                        {
                            "rateKey": "20160411|20160412|W|71|87399|DBL.2D|CGW-TODOS1|BB||1~2~1|2|N@-644903865",
                            "rateClass": "NOR",
                            "rateType": "BOOKABLE",
                            "net": "687.31",
                            "discount": "111.89",
                            "discountPCT": "14.00",
                            "sellingRate": "799.20",
                            "rateComments": "Incluye desayuno americano para adulto y menor\nel hotel no cuenta con aire acondicionado  ",
                            "paymentType": "AT_WEB",
                            "packaging": false,
                            "boardCode": "BB",
                            "boardName": "ALOJAMIENTO Y DESAYUNO",
                            "cancellationPolicies": [
                                {
                                    "amount": "799.20",
                                    "from": "2016-04-08T23:59:00-05:00"
                                }
                            ],
                            "rateBreakDown": {
                                "rateDiscounts": [
                                    {
                                        "code": "DN",
                                        "name": "Descuento Niño",
                                        "amount": "-281.68"
                                    }
                                ]
                            },
                            "rooms": 1,
                            "adults": 2,
                            "children": 1,
                            "childrenAges": "2",
                            "offers": [
                                {
                                    "code": "9001",
                                    "name": "Descuento niños",
                                    "amount": "-281.68"
                                }
                            ]
                        }
                    ]
                }
            ],
            "totalSellingRate": "799.20",
            "totalNet": "687.31",
            "currency": "MXN"
        }
    },
    {
        "auditData": {
            "processTime": "1543",
            "timestamp": "2016-04-08 04:33:19.469",
            "requestHost": "66.226.74.194",
            "serverId": "sa3RKSJACHXE79K.env",
            "environment": "[int]",
            "release": "2cb1bad878d2195c9b508e2007ef96616007dacb",
            "internal": "bz66k2etuxrt5q5h2zyf3jf6|MX|03|HA|1|3|0|3|3|N|||||||||1"
        },
        "hotel": {
            "checkIn": "2016-04-11",
            "checkOut": "2016-04-12",
            "code": 87399,
            "name": "Premier",
            "categoryCode": "3EST",
            "categoryName": "3 ESTRELLAS",
            "destinationCode": "MDF",
            "destinationName": "Ciudad de Mexico",
            "zoneCode": 10,
            "zoneName": "Downtown",
            "latitude": "19.431353",
            "longitude": "-99.156457",
            "rooms": [
                {
                    "code": "SGL.DB",
                    "name": "INDIVIDUAL CAMA DOBLE",
                    "rates": [
                        {
                            "rateKey": "20160411|20160412|W|71|87399|SGL.DB|CGW-TODOS1|BB||1~1~1|5|N@-644903865",
                            "rateClass": "NOR",
                            "rateType": "BOOKABLE",
                            "net": "687.31",
                            "discount": "111.89",
                            "discountPCT": "14.00",
                            "sellingRate": "799.20",
                            "rateComments": "Incluye desayuno americano para adulto y menor\nel hotel no cuenta con aire acondicionado  ",
                            "paymentType": "AT_WEB",
                            "packaging": false,
                            "boardCode": "BB",
                            "boardName": "ALOJAMIENTO Y DESAYUNO",
                            "cancellationPolicies": [
                                {
                                    "amount": "799.20",
                                    "from": "2016-04-08T23:59:00-05:00"
                                }
                            ],
                            "rateBreakDown": {
                                "rateDiscounts": [
                                    {
                                        "code": "DN",
                                        "name": "Descuento Niño",
                                        "amount": "-641.98"
                                    }
                                ]
                            },
                            "rooms": 1,
                            "adults": 1,
                            "children": 1,
                            "childrenAges": "5",
                            "offers": [
                                {
                                    "code": "9001",
                                    "name": "Descuento niños",
                                    "amount": "-641.98"
                                }
                            ]
                        }
                    ]
                }
            ],
            "totalSellingRate": "799.20",
            "totalNet": "687.31",
            "currency": "MXN"
        }
    }
]

我尝试使用for (var key in data),其中data是来自AJAX的JSON响应。

3 个答案:

答案 0 :(得分:0)

这是一个数组。您应该使用正常的for循环进行迭代,例如

for(var i = 0; i < data.length;i++) { 
   var rooms = data[i].hotel.rooms 
   for(var j = 0; j < rooms.length; j++) {
     var rates = rooms.rates .. //do something with rates, it's also an array
   }
}

答案 1 :(得分:0)

每次看到[]时,使用每个循环,字符串中的第一个字符是括号,所以我们在每个案例中选择一个孩子&#34; hotel&#34;使用点符号v.hotel,v。酒店不会以括号开头,所以我们不需要循环,我们在json对象中向下,直到我们到达rates键,我们看到另一个braket所以我们循环并在我们的例子中选择子元素rateKey

    var obj =[
    {
        "auditData": {
            "processTime": "1545",
            "timestamp": "2016-04-08 04:33:17.145",
            "requestHost": "66.226.74.194",
            "serverId": "sa37AUX3ROLBLIS.env",
            "environment": "[int]",
            "release": "2cb1bad878d2195c9b508e2007ef96616007dacb",
            "internal": "bz66k2etuxrt5q5h2zyf3jf6|MX|03|HA|1|3|0|1|3|N|||||||||1"
        },...];

$.each(obj, function(i, v) {

  $.each(v.hotel.rooms, function(i1, v1) {
    $.each(v1.rates, function(i2, v2) {
      console.log(v2.rateKey);
    });
  })
});

https://jsfiddle.net/78cpwq5g/

答案 2 :(得分:0)

使用Json对象。 如果假设值存储在JsonObject中。

您可能想要检索一些值。 例如: - “code”:87399,         “名字”:“总理”,         “categoryCode”:“3EST”,         “categoryName”:“3 ESTRELLAS”,         “destinationCode”:“MDF”,         “destinationName”:“Ciudad de Mexico”,

$(JsonObject).each(function(){
     $(this).hotel.code;
     $(this).hotel.name;    
})