$ .each不以给定顺序呈现JSON

时间:2014-12-12 05:12:13

标签: jquery

这是我的服务器JSON

"bids":{"51200512":{"bidAmt":"Rs.63,736,494.00","date":"2014 12 12 09:49:59 AM","bidder":"Amit"},

"69120512":{"bidAmt":"Rs.734,294.00","date":"2014 12 12 09:51:29 AM","bidder":"Tata India Pvt Ltd"},

"66560256":{"bidAmt":"Rs.87,265.00","date":"2014 12 12 09:52:31 AM","bidder":"Tata Telecommunication servoices <i>asas</i> Telec"},

"69120768":{"bidAmt":"Rs.63,737.00","date":"2014 12 12 09:52:05 AM","bidder":"Tata India Pvt Ltd"},

"69120256":{"bidAmt":"Rs.63,736.00","date":"2014 12 12 09:50:50 AM","bidder":"Tata India Pvt Ltd"}}}

在火狐中它显示正确的顺序,即

Rs.63,736,494.00
Rs.734,294.00
Rs.87,265.00
Rs.63,737.00
Rs.63,736.00

但是在chrome:

Rs.63,736,494.00
Rs.734,294.00
Rs.63,736.00
Rs.734,294.00
Rs.63,737.00

以下是我如何解析我的JSON

 $.each(bids,function(k,v){
       _tbl+="<tr bd='y'><td><a href=\""+_this.options.context+"/profile/"+k+"\">"+v.bidder+"</a><br/><small><span class='timeago' title='"+v.date+"'>"+v.date+"</span><small></td><td>"+v.bidAmt+"</td></tr>";
 });}

$("#showBidModel table").append(_tbl);

1 个答案:

答案 0 :(得分:7)

Javascript不保证对象的顺序。请参阅Does JavaScript Guarantee Object Property Order?

Chrome首先按排序顺序使用数字键迭代它们。如果需要显式排序,请使用数组。

"bids": [
    {"id": "69120512", "bidAmt":"Rs.734,294.00","date":"2014 12 12 09:51:29 AM","bidder":"Tata India Pvt Ltd"},
    {"id": "66560256", "bidAmt":"Rs.87,265.00","date":"2014 12 12 09:52:31 AM","bidder":"Tata Telecommunication servoices &lt;i&gt;asas&lt;/i&gt; Telec"},
    ...
]