使用for in循环遍历json对象的数据


使用for in遍历json对象数据,如果数据中的名称有为数字的话,只对正整数有效,那么先会输出为正整数的数据,后面其他的会按照原来数据中定义的顺序不变输出。

  • 针对名称为数字的json对象数据进行测试,代码如下:
<script type="text/javascript">

 var json_obj_data = 
{
    "40":{"title":"all" , "name" :"all promotions" ,"id" :"all"},
    "20":{"title":"best_room_rate" , "name" : "best room rate" , "id" : "best_room_rate"},
    "10":{"title":"summer rate" , "name" : "summer rate" , "id" : "30"}    
}
//40,20,10可以不加双引号括住,输出的结果是一样的
for(var data in json_obj_data){ alert(data);//10->20->40 } </script>
  • 针对名称为字符串的json对象数据进行测试,代码如下:
var json_data = {"3promotion":{"title":"all","room_promotion_id":"all"},
                             "2promotion":{"title":"best_room_rate","roompromotion_id":"best_room_rate"},
                            "1promotion":{"title":"Summer Room rate" , "room_promotion_id":"30"}
                            };
            
for(var promotion in json_data){
          alert(promotion);//输出结果是按照上面定义的顺序
}
  • 针对名称为数字和字符串的json对象数据测试,代码如下:
var json_data = {"3promotion":{"title":"all","room_promotion_id":"all"},
                             "2promotion":{"title":"best_room_rate","roompromotion_id":"best_room_rate"},
                            "1":{"title":"Summer Room rate" , "room_promotion_id":"30"}
                            };
            
 for(var promotion in json_data){
        alert(promotion);//数据先输出的是名称为数字的,然后就按照顺序输出名称为字符串的
 }

具有两个名称为数字的,其他为字符串的json对象数据测试,代码如下:

var json_data = {"3promotion":{"title":"all","room_promotion_id":"all"},
                             "2promotion":{"title":"best_room_rate","roompromotion_id":"best_room_rate"},
                            "1":{"title":"Summer Room rate" , "room_promotion_id":"30"},
                            "0":{"title":"expecial rate" , "room_promotion_id":"60"}
                            };
            
for(var promotion in json_data){
      alert(promotion);//输出结果为0->1->3promotion->2promotion
}

具有名称为数字,其中有为负数的将会被当作字符串处理,其他为字符串的json对象数据测试,代码如下:

var json_data = {"3promotion":{"title":"all","room_promotion_id":"all"},
                             "2promotion":{"title":"best_room_rate","roompromotion_id":"best_room_rate"},
                            "1":{"title":"Summer Room rate" , "room_promotion_id":"30"},
                            "0":{"title":"expecial rate" , "room_promotion_id":"60"},
                            "-1":{"title":‘demo3‘,‘room_promotion_id‘:"demo3"}
                            };
            
 for(var promotion in json_data){
       alert(promotion);//0->1->3promotion->2promotion->-1
 }

 包括小于1的数字名称,以及数字为负数的名称,代码测试如下:

var json_data = {"3promotion":{"title":"all","room_promotion_id":"all"},
                             "2promotion":{"title":"best_room_rate","roompromotion_id":"best_room_rate"},
                            "1":{"title":"Summer Room rate" , "room_promotion_id":"30"},
                            "0.25":{"title":"expecial rate" , "room_promotion_id":"60"},
                            "-1":{"title":‘demo3‘,‘room_promotion_id‘:"demo3"}
                            };
            
for(var promotion in json_data){
    alert(promotion);//1->3promotion->2promotion->0.25->-1
}

 

使用for in循环遍历json对象的数据,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。