如何使用for循环与对象循环Restfulwebservice

时间:2015-01-19 17:38:52

标签: javascript json for-loop

如何使用单个对象的for循环遍历Restfulwebserivice。并且我不想在每个循环中使用,所以我该怎么做。我无法使用

获取数据
  for(var key in data) {
                    alert(data[key].hotel_geo_node.name);
             if(data.hasOwnProperty(key)){
           }

这是api     {       "数据":{         " 4325474491990470056":{             " hotel_geo_node":{                 "姓名":"国会大厦",                 "标签":{                     " hotel_chain_code":[                         " nichendgrp"                     ]                     " property_type":[                         "酒店"                     ]                     " property_budget_category":[                         "豪华"                     ]                     "其他人":[                         " goibibo_hotel"                     ]                 },

    }, 

   "4325474491990470057": {
        "hotel_geo_node": {
            "name": "The Capitol", 
            "tags": {
                "hotel_chain_code": [
                    "nichendgrp"
                ], 
                "property_type": [
                    "Hotel"
                ], 
                "property_budget_category": [
                    "Luxury"
                ], 
                "others": [
                    "goibibo_hotel"
                ]
            }, 

    }, 
    "4325474491990470058": {
        "hotel_geo_node": {
            "name": "The Capitol", 
            "tags": {
                "hotel_chain_code": [
                    "nichendgrp"
                ], 
                "property_type": [
                    "Hotel"
                ], 
                "property_budget_category": [
                    "Luxury"
                ], 
                "others": [
                    "goibibo_hotel"
                ]
            }, 
    }
 }

}

1 个答案:

答案 0 :(得分:0)

您无法使用"标准"循环访问对象的键。 for循环,因为它不是数组,它的键不是数字。你只能使用for-in循环循环抛出它:

for(var key in data) {
    if(data.hasOwnProperty(key)){ // check if 'key' property is in object 'data' and not in it's prototype chain
        doTheStuff(data[key]);
    }
}