码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-08-08 20:57:36      阅读:466      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   java   使用   io   数据   for   


使用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对象的数据,布布扣,bubuko.com

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

标签:style   blog   color   java   使用   io   数据   for   

原文地址:http://www.cnblogs.com/shuman/p/3899977.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!