首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery多层JSON

问jQuery多层JSON
EN

Stack Overflow用户
提问于 2017-05-28 05:36:11
回答 1查看 106关注 0票数 0

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

代码语言:javascript
复制
{
    "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。

代码语言:javascript
复制
jQuery.each(data, function(index, value){
    alert(value['WarrSalePrice']);    //This get the individual sales prices
});

问题是我需要得到RowCount和TotalSalesPrice。我的逻辑是不把它做好。它告诉我,我的价值,如果“未定义”的第五(总计行)。我还需要在调用RowCount之前读取jQuery.each值。因为如果RowCount == 0,则不需要执行jQuery.each。谁能给我指明正确的方向吗。

祝你好运,乔治

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-28 05:43:28

https://jsfiddle.net/mvpb1k1r/

检查一下它是否未定义。

代码语言:javascript
复制
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
    }
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44224161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档