如何使用jquery迭代这个json消息的嵌套数组(即“orders”)?
[{"Type":"X","Symbol":"AAP","BidPrice":17.0,
"OfferPrice":0.0,"BidQty":9,"OfferQty":0,"LastTradePrice":17.0,
"orders":[{"BidQuantity":"12","BidPrice":"17.0","OfferQuantity":"","OfferPrice":""},
"BidQuantity":"5","BidPrice":"17.0","OfferQuantity":"","OfferPrice":""},
"BidQuantity":"5","BidPrice":"17.0","OfferQuantity":"","OfferPrice":""},
"BidQuantity":"2","BidPrice":"16.0","OfferQuantity":"","OfferPrice":""}]}
我尝试过以下方式但不起作用
$.each(data, function(i, item) {
$('#last').text(data[i].LastTradePrice);
var ords = item.orders;
$.each(ords, function(x,ork) {
alert(ork[x].BidPrice);
});
});
我在firefox控制台中收到以下错误
ork [x]未定义
答案 0 :(得分:1)
您的json在"orders:[
无效,应该是"orders":[
,您也忘了将orders
数组中的每个对象都包含}
。在任何情况下,下面都会更正JSON。
在任何情况下,您都不需要使用jquery来做这样的事情:
for(var i = 0; i < inputs.length; i++) {
alert(inputs[i].BidPrice);
}
Orders是类型数组,我严重怀疑你需要使用jQuery,但无论如何: http://jsfiddle.net/kyWGT/14/
var data =
[{"Type":"X","Symbol":"AAP","BidPrice":17.0,
"OfferPrice":0.0,"BidQty":9,"OfferQty":0,"LastTradePrice":17.0,
"orders":[{"BidQuantity":"12","BidPrice":"17.0","OfferQuantity":"","OfferPrice":""},
{"BidQuantity":"5","BidPrice":"17.0","OfferQuantity":"","OfferPrice":""},
{"BidQuantity":"5","BidPrice":"17.0","OfferQuantity":"","OfferPrice":""}, {"BidQuantity":"2","BidPrice":"16.0","OfferQuantity":"","OfferPrice":""}]
}];
$.each(data, function(i, item) {
$('#last').text(data[i].LastTradePrice);
var ords = item.orders;
$.each(ords, function(i, ork) {
alert(ork.BidPrice);
});
});
答案 1 :(得分:1)
$.each
使用索引和项调用回调。该项不需要具有名为该索引的属性,该数据具有。
$.each(data, function(i, item) {
item === data[i];
})
因此,请勿在代码中使用data[i]
,item
,并且不要使用ords[x]
或ork[x]
,而应使用ork
。另外:如果您不需要,请不要使用迭代器功能! Javascript具有内置循环语法: - )
for (var i=0; i<data.length; i++) {
$('#last').text(data[i].LastTradePrice);
var ords = data[i].orders;
for (var j=0; j<ords.length; j++)
alert(ords[j].BidPrice);
}
......这要快得多。此外,如果您知道结果数据只有一个项目,则可以省略外部循环并使用data[0]
。
答案 2 :(得分:1)
你的JSON还有其他错误,我认为正确的JSON就是:
[{"Type":"X","Symbol":"AAP","BidPrice":17.0,
"OfferPrice":0.0,"BidQty":9,"OfferQty":0,"LastTradePrice":17.0,
"orders":[{"BidQuantity":"12","BidPrice":"17.0","OfferQuantity":"","OfferPrice":""},
{"BidQuantity":"5","BidPrice":"17.0","OfferQuantity":"","OfferPrice":""},
{"BidQuantity":"5","BidPrice":"17.0","OfferQuantity":"","OfferPrice":""},
{"BidQuantity":"2","BidPrice":"16.0","OfferQuantity":"","OfferPrice":""}]}]
假设这是对的,你的jQuery几乎就在那里。在第二个回调中,'ork'是订单对象,您不需要用'x'对其进行索引。这应该有效:
$.each(ords, function(x,ork) {
alert(ork.BidPrice);
});
如果您复制/粘贴此代码,它应该有用,它适用于我:
var data = [{"Type":"X","Symbol":"AAP","BidPrice":17.0,
"OfferPrice":0.0,"BidQty":9,"OfferQty":0,"LastTradePrice":17.0,
"orders":[{"BidQuantity":"12","BidPrice":"17.0","OfferQuantity":"","OfferPrice":""},
{"BidQuantity":"5","BidPrice":"17.0","OfferQuantity":"","OfferPrice":""},
{"BidQuantity":"5","BidPrice":"17.0","OfferQuantity":"","OfferPrice":""},
{"BidQuantity":"2","BidPrice":"16.0","OfferQuantity":"","OfferPrice":""}]}]
$.each(data, function(i, item) {
var ords = item.orders;
$.each(ords, function(i, ork) {
alert(ork.BidPrice);
});
});
答案 3 :(得分:1)
假设这是你的Json
var sbk =
[{"Type":"X","Symbol":"AAP","BidPrice":17.0,
"OfferPrice":0.0,"BidQty":9,"OfferQty":0,"LastTradePrice":17.0,
"orders":[{"BidQuantity":"12","BidPrice":"17.0","OfferQuantity":"","OfferPrice":""},
{"BidQuantity":"5","BidPrice":"17.0","OfferQuantity":"","OfferPrice":""},
{"BidQuantity":"5","BidPrice":"17.0","OfferQuantity":"","OfferPrice":""},
{"BidQuantity":"2","BidPrice":"16.0","OfferQuantity":"","OfferPrice":""}] }];
这是你的剧本:
$.each(sbk, function(i, item) {
var ords = item.orders;
$.each(ords, function(i, ork) {
console.log(ork.BidPrice);
alert(ork.BidPrice);
});
答案 4 :(得分:0)
将我的代码更改为以下方式,它对我非常有用。
$.each(data, function(i, item) {
alert(data[i].Symbol); /* retrieve values from first json array */
if(data[i].orders != null){
for(var j=0; j<data[i].orders.length; j++){
alert(item.orders[j].BidPrice);/* retrieve json values from nested array */
}
}
}
对不起因答案延迟回复,我正忙着工作一段时间...... 感谢所有试图提供帮助的人。