晚上好,我正在处理一个JSON,我必须解析这个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。我的逻辑是不把它做好。它告诉我,我的价值,如果“未定义”的第五(总计行)。我还需要在调用RowCount之前读取jQuery.each值。因为如果RowCount == 0,则不需要执行jQuery.each。谁能给我指明正确的方向吗。
祝你好运,乔治
发布于 2017-05-28 05:43:28
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
}
});https://stackoverflow.com/questions/44224161
复制相似问题