使用Jquery检索JSON值以进行计算

时间:2012-02-07 15:47:00

标签: javascript json

我有一个计算表单,它从JSON

获取其值
    var shirtsJSON = [
        {"pattern": "Delta Adult S/S 5.2oz", "basePrice": 5.68,
            "sizes": {"s-xl": 0, "xxl": 0.84},
            "colors": {"white": 0, "athletic": 0.12, "color": 0.23},
            "numColors": {"1-2": 0, "3-4": 1.60, "5-6": 3.18, "7-8": 4.81, "9-10": 6.39},
            "deductions": {"de48pp": 0, "de72pp": .9, "de96pp": 1.35, "de144pp": 2.37, "de288pp": 2.65},
            "oneLocation": {"onelocnone": 0, "oneloc12": 3.28, "oneloc34": 5.41, "oneloc56": 7.52, "oneloc78": 9.69, "oneloc910": 11.80}, 
            "twoLocation": {"twolocnone": 0, "twoloc12": 3.28, "twoloc34": 5.41, "twoloc56": 7.52, "twoloc78": 9.69, "twoloc910": 11.80},
            "threeLocation": {"threelocnone": 0, "threeloc12": 3.28, "threeloc34": 5.41, "threeloc56": 7.52, "threeloc78": 9.69, "threeloc910": 11.80},
            "fourLocation": {"fourlocnone": 0, "fourloc12": 3.28, "fourloc34": 5.41, "fourloc56": 7.52, "fourloc78": 9.69, "fourloc910": 11.80}},

        {"pattern": "Delta Adult S/S 6.1oz", "basePrice": 6.68,
            "sizes": {"s-xl": 0, "xxl": 0.84},
            "colors": {"white": 0, "athletic": 0.12, "color": 0.23},
            "numColors": {"1-2": 0, "3-4": 1.60, "5-6": 3.18, "7-8": 4.81, "9-10": 6.39},
            "deductions": {"de48pp": 0, "de72pp": .70, "de96pp": 1.25, "de144pp": 2.47, "de288pp": 2.55},
            "oneLocation": {"onelocnone": 0, "oneloc12": 3.28, "oneloc34": 5.41, "oneloc56": 7.52, "oneloc78": 9.69, "oneloc910": 11.80}, 
            "twoLocation": {"twolocnone": 0, "twoloc12": 3.28, "twoloc34": 5.41, "twoloc56": 7.52, "twoloc78": 9.69, "twoloc910": 11.80},
            "threeLocation": {"threelocnone": 0, "threeloc12": 3.28, "threeloc34": 5.41, "threeloc56": 7.52, "threeloc78": 9.69, "threeloc910": 11.80},
            "fourLocation": {"fourlocnone": 0, "fourloc12": 3.28, "fourloc34": 5.41, "fourloc56": 7.52, "fourloc78": 9.69, "fourloc910": 11.80}},

然后我在这里得到最终结果

            $('#48pp').html("Per Piece : " + currency + getMoney(totalPrice));
            $('#72pp').html("Per Piece : " + currency + getMoney(totalPrice));
            $('#96pp').html("Per Piece : " + currency + getMoney(totalPrice));
            $('#144pp').html("Per Piece : " + currency + getMoney(totalPrice));
            $('#288pp').html("Per Piece : " + currency + getMoney(totalPrice));

我希望它从我尝试这样做的推理JSON中减去值:

$('#72pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON.deductions[0].de72pp));

但它没有用,我似乎无法弄明白。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

shirtsJSON.deductions不是数组,因此尝试索引不会检索任何内容。

请尝试使用shirtsJSON.deductions.de72pp,因为de72ppshirtsJSON.deductions对象的属性。

另外,从[行中删除var shirtsJSON = [{

答案 1 :(得分:0)

我认为[0]位置错误 - 在shirtsJSON之后尝试而不是deductions

$('#72pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[0].deductions.de72pp));

而不是

$('#72pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON.deductions[0].de72pp));

这是因为shirtsJSON是一个数组,而deductions不是。

修改

检索多个deductions的循环的快速示例。这假定每个deductions部分每次都包含相同的信息(例如,它们都包含48pp72pp等。

for(i = 0; i < shirtsJSON.length; i++) {
    $('#72pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[i].deductions.de72pp));
    ...etc...
}

注意i如何替换0中的shirtsJSON[0]。您需要稍微更改代码,以便.append编辑(这会添加到该元素中的任何内容的末尾),而不是使用.html(它会覆盖该元素中的任何内容)。

编辑2:

以下是您可能会发现有用的链接:http://blog.xkoder.com/2008/07/10/javascript-associative-arrays-demystified/