gpt4 book ai didi

jquery - 如何使用 jquery 迭代 json 消息的嵌套数组?

转载 作者:行者123 更新时间:2023-12-01 03:20:59 25 4
gpt4 key购买 nike

如何使用 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] 未定义

最佳答案

您的 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);
});
});​

关于jquery - 如何使用 jquery 迭代 json 消息的嵌套数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9991442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com