gpt4 book ai didi

javascript - jQuery 多层 JSON

转载 作者:行者123 更新时间:2023-12-03 04:13:52 24 4
gpt4 key购买 nike

晚上好,我正在处理一个 JSON,我必须解析它才能得到我需要的东西。它看起来像这样:

{
"0":{"ID":"282","WarrSalePrice":"1.00"}
,"1":{"ID":"283","WarrSalePrice":"2.00"}
,"2":{"ID":"284","WarrSalePrice":"3.00"}
,"3":{"ID":"285","WarrSalePrice":"4.00"}
,"Totals":{"RowCount":"4","WarrSalePrice":"10.00"}
}

我使用以下代码来遍历上面的 JSON。

jQuery.each(data, function(index, value){
alert(value['WarrSalePrice']); //This get the individual sales prices
});

问题是我需要获取 RowCount 和 TotalSalesPrice。我的逻辑是没有完成它。它告诉我,我的值如果为第五(总计行)“未定义”。我还需要在调用 jQuery.each 之前读取 RowCount 值。因为如果 RowCount == 0,则无需执行 jQuery.each。请有人给我指出正确的方向。

祝一切顺利,乔治

最佳答案

https://jsfiddle.net/mvpb1k1r/

只需检查它是否未定义。

var data = {
"0":{"ID":"282","WarrSalePrice":"1.00"}
,"1":{"ID":"283","WarrSalePrice":"2.00"}
,"2":{"ID":"284","WarrSalePrice":"3.00"}
,"3":{"ID":"285","WarrSalePrice":"4.00"}
,"Totals":{"RowCount":"4","WarrSalePrice":"10.00"}
}

jQuery.each(data, function(index, value){
if(typeof value['WarrSalePrice'] != "undefined") {
console.log("WarrSalePrice: " + value['WarrSalePrice']); //This get the individual sales prices
}

if(typeof value['RowCount'] != "undefined") {
console.log("RowCount: " + value['RowCount']); //This get the individual sales prices
}
});

关于javascript - jQuery 多层 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44224161/

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