标签:style blog color java 使用 io strong 数据
//php中的数组数据格式 Array ( [all] => Array ( [title] => ALL [room_promotion_id] => all ) [best_available_rate] => Array ( [title] => Best Available Rate [room_promotion_id] => best_available_rate ) [30] => Array ( [room_promotion_id] => 30 [version] => 1 [date_from] => 2014-08-05 [date_to] => 2014-09-30 [night_no] => 1 [discount_stype] => 1 [discount_rate] => 10 [discount_night] => 0 [title] => Summer Discount - Premier Suite [summary] => <p> Bring your family along to relax in the Summer! Enjoy a 10% off whenever you book Premier Suite.</p> [before_day] => 0 ) )
//json_encode()之后输出的数据,这也是ajax返回的json数据对象 {"all":{"title":"ALL","room_promotion_id":"all"},
"best_available_rate":{"title":"Best Available Rate","room_promotion_id":"best_available_rate"},
"30":{"room_promotion_id":"30","version":"1","date_from":"2014-08-05","date_to":"2014-09-30","night_no":"1","discount_stype":"1",
"discount_rate":"10","discount_night":"0","title":"Summer Discount - Premier Suite",
"summary":"<p>\r\n\tBring your family along to relax in the Summer! Enjoy a 10% off whenever you book Premier Suite.<\/p>\r\n","before_day":"0"}}
//javascript代码 function ajaxPrmotion(){ var check_in_hidden = $(‘#check_in_hidden‘).val(); var check_out_hidden = $(‘#check_out_hidden‘).val(); var adults_num = $(‘#adult‘).attr(‘selected‘,‘selected‘).val(); var child_num = $(‘#child‘).attr(‘selected‘,‘selected‘).val(); var promotion_opt =‘‘; var url= "reservation.php?action_type=mobile_ajax&check_in="+check_in_hidden+"&check_out="+check_out_hidden+"&adult="+adults_num+"&child="+child_num+"&upd=1"; $.ajax({ url:url, type : "get", dataType : "json", success: function(result) { //$("select[name=promotion_id]").find("option").remove().end().append("<option value=‘0‘>__($lang_text.best_rate)__</option>"); $("select[name=promotion_id]").find("option").remove(); var promotion_opt =‘‘; if (result!=‘‘ && result){ /×
使用for(var variablename in object){}循环出来的数据顺序与php中打印输出的数组中数据的顺序和json_encode()后的数据顺序是不同
最先循环取到的索引为30的数据,然后是all和best_available_rate,不知道这个究竟是怎么一回事。
因为在php中的数组中的顺序已经固定好了,json_encode()出来的数据也是和php数组中的顺序相同,但是为何在javascript中使用for in出来的数据
顺序反而不相同呢?只有两个原因: 1.jquery中对传输的json格式数据进行了排序吗?2.要么就是在for in发生的问题了
×/
for(var promotion in result){ promotion_opt += ‘<option value="‘ +result[promotion]["room_promotion_id"]+‘">‘+ result[promotion]["title"]+‘</option>‘; } } $("select[name=promotion_id]").append(promotion_opt); } }); }
所以,我自己写一段javascript代码来进行测试,发现问题的原因是在javascript的for in上面.
测试代码如下:
<script type="text/javascript"> var json_data = {"all":{"title":"all","room_promotion_id":"all"}, "best_room_rate": {"title":"best_room_rate","roompromotion_id":"best_room_rate"}, "30":{"title":"Summer Room rate" , "room_promotion_id":"30"} }; for(var promotion in json_data){ alert(promotion);// 30->all->best_room_rate }
</script>
针对上面的代码,我将"30"这个数字修改为"summer rate",代码如下:
<script type="text/javascript"> var json_data = {"all":{"title":"all","room_promotion_id":"all"}, "best_room_rate":{"title":"best_room_rate","roompromotion_id":"best_room_rate"}, "summer rate":{"title":"Summer Room rate" , "room_promotion_id":"30"} }; for(var promotion in json_data){ alert(promotion); //这次循环输出的结果完全就是按照上面定义的json_data数据中顺序 } </script>
从上面的代码比较,我得出下面的结论,也许我的结论不一定是完全正确和合乎科学的。
结论:当json格式数据中既有包含有数字索引和文字索引时,那么在for in循环中首先取出来必定是数字索引的数据,然后就是按顺序取出文字索引的数据。
Ajax请求php返回json对象数据中包含有数字索引和字符串索引,在for in循环中取出数据的顺序问题,布布扣,bubuko.com
Ajax请求php返回json对象数据中包含有数字索引和字符串索引,在for in循环中取出数据的顺序问题
标签:style blog color java 使用 io strong 数据
原文地址:http://www.cnblogs.com/shuman/p/3899824.html