2012-02-07 54 views
1

我有一個計算的形式,從一個JSON獲取其值檢索JSON值與jQuery的計算

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)); 

但它沒有工作,我似乎無法如圖退出。誰能幫忙?

+0

'shirtsJSON [0] .deductions' - 這是很難說沒有看到其餘的代碼和JSON。 – 2012-02-07 15:52:39

+0

您的JSON格式不正確。請參閱:http://jsonlint.com/ – 2012-02-07 15:54:17

回答

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是如何在shirtsJSON[0]取代0。您需要稍微更改一下代碼,以便它可以代替使用.html(它將覆蓋該元素中的所有內容),從而使其可以添加到該元素的末尾。

編輯2:

這裏,你可能會發現有用的鏈接:我想你想http://blog.xkoder.com/2008/07/10/javascript-associative-arrays-demystified/

+0

工程太棒了!謝謝!!! – ehilse 2012-02-07 16:01:06

+0

我對此有另外一個問題...如果我想要使用不同演繹模式的代碼不會工作,我需要它與衆不同嗎?我猜索引是什麼造成的。如果它是一個愚蠢的問題,我還是有點新的感覺。 – ehilse 2012-02-07 16:15:32

+0

在你的例子中,'shirtsJSON'是一個包含幾個關聯數組(每個表示同一個產品的不同模式)的索引數組(我將假設它代表一個單一產品)。爲了得到它們中的每一個的扣除,你可以循環遍歷'shirtsJSON'索引數組,可能帶有for循環,隨時檢索關於每個單獨模式的信息。 – johneth 2012-02-07 16:32:07

1

shirtsJSON.deductions不是一個數組,所以嘗試索引不會檢索任何東西。

嘗試shirtsJSON.deductions.de72pp而不是de72ppshirtsJSON.deductions對象的屬性。

另外,從var shirtsJSON = [{行刪除[行。

+0

每當我沒有索引它時,它就會導致代碼中斷。 – ehilse 2012-02-07 16:01:13