2014-01-15 40 views
0

我使用的是Shopify,必須將Qubit添加到客戶端。如何以編程方式將東西添加到JS對象?

在任何時候都有一個「籃子」對象,顯示用戶購物車中的產品。

如何將對象動態插入到此json結構中?

"line_items": [{ 
     "product": { 
     "id": "1234567890", 
     "sku_code": "0987654321", 
     "url": "product.html", 
     "name": "Sparkly Shoes", 
     "description": "Description about this product", 
     "manufacturer": "The Shoe Co", 
     "category": "Shoe", 
     "subcategory": "Heels", 
     "color": "n/a", 
     "stock": 3, 
     "size": "6", 
     "currency": "GBP", 
     "unit_price": 130, 
     "unit_sale_price": 130, 
     "voucher": "MYVOUCHER1" 
     }, 
     "quantity": 1, 
     "subtotal": 130, 
     "total_discount": 0 
    }, { 
     "product": { 
     "id": "1234567890", 
     "sku_code": "0987654321", 
     "url": "product-dress.html", 
     "name": "Red Dress", 
     "description": "Description about this product", 
     "manufacturer": "The Dress Co", 
     "category": "Dresses", 
     "subcategory": "Red dresses", 
     "color": "n/a", 
     "stock": 3, 
     "size": "6", 
     "currency": "GBP", 
     "unit_price": 200, 
     "unit_sale_price": 150, 
     "voucher": "MYVOUCHER1" 
     }, 
     "quantity": 1, 
     "subtotal": 200, 
     "total_discount": 50 
    }, { 
     "product": { 
     "id": "1234567890", 
     "sku_code": "0987654321", 
     "url": "product-swimwear.html", 
     "name": "Bikini", 
     "description": "Description about this product", 
     "manufacturer": "The Bikini Co", 
     "category": "Swimwear", 
     "subcategory": "Bikini", 
     "color": "n/a", 
     "stock": 3, 
     "size": "6", 
     "currency": "GBP", 
     "unit_price": 100, 
     "unit_sale_price": 100, 
     "voucher": "MYVOUCHER1" 
     }, 
     "quantity": 1, 
     "subtotal": 100, 
     "total_discount": 0 
    }] 
    }, 

來自http://tools.qubitproducts.com/uv/demosite/guide.html

它可以是一種產品,兩種產品,等...我將如何動態地通過在購物車中的可用產品迭代(僞代碼是好的),然後顯示幾個值?


如果我定義了上面的json結構,我該如何動態地做到這一點?只需在代碼之間放置一個循環? (覺得髒嗎?)

+0

我想你可能會把JSON(一種序列化格式)與Javascript對象(你可以操縱的運行時事情)混淆起來。你可以舉一些你正在考慮什麼樣的代碼的例子嗎? – hugomg

+0

您是否問過在獲得產品數據字段後如何將產品添加到購物車? –

回答

1

您提供的代碼不是JSON。這是一個javascript對象,因爲missingno聲明缺少{。您可以使用Kelly J Andrews答案將項目添加到對象或複製對象並向其添加項目以創建更新的對象。爲了得到一個對象JSON,您可以使用JSON.stringify()

var jsonString = JSON.stringify({"obj":{"param":"val"}}); 

var obj = {"obj":{"param":"val"}}; 
var jsonString = JSON.stringify(obj); 

但在你的情況,你已經有一個對象。您提供的代碼有換行符,而不是一個字符串,所以我們可以很容易地拋出一個var obj =(加上{你離開了),並有:

var obj = 
{ "line_items": 
    [{ 
    "product": { 
     "id": "1234567890", 
     "sku_code": "0987654321", 
     "url": "product.html", 
     "name": "Sparkly Shoes", 
     "description": "Description about this product", 
     "manufacturer": "The Shoe Co", 
     "category": "Shoe", 
     "subcategory": "Heels", 
     "color": "n/a", 
     "stock": 3, 
     "size": "6", 
     "currency": "GBP", 
     "unit_price": 130, 
     "unit_sale_price": 130, 
     "voucher": "MYVOUCHER1" 
    }, 
    "quantity": 1, 
    "subtotal": 130, 
    "total_discount": 0 
    }, { 
    "product": { 
     "id": "1234567890", 
     "sku_code": "0987654321", 
     "url": "product-dress.html", 
     "name": "Red Dress", 
     "description": "Description about this product", 
     "manufacturer": "The Dress Co", 
     "category": "Dresses", 
     "subcategory": "Red dresses", 
     "color": "n/a", 
     "stock": 3, 
     "size": "6", 
     "currency": "GBP", 
     "unit_price": 200, 
     "unit_sale_price": 150, 
     "voucher": "MYVOUCHER1" 
    }, 
    "quantity": 1, 
    "subtotal": 200, 
    "total_discount": 50 
    }, { 
    "product": { 
     "id": "1234567890", 
     "sku_code": "0987654321", 
     "url": "product-swimwear.html", 
     "name": "Bikini", 
     "description": "Description about this product", 
     "manufacturer": "The Bikini Co", 
     "category": "Swimwear", 
     "subcategory": "Bikini", 
     "color": "n/a", 
     "stock": 3, 
     "size": "6", 
     "currency": "GBP", 
     "unit_price": 100, 
     "unit_sale_price": 100, 
     "voucher": "MYVOUCHER1" 
    }, 
    "quantity": 1, 
    "subtotal": 100, 
    "total_discount": 0 
    }] 
}; 

現在我們可以用凱利的解決方案,並添加東西的對象:

var productToAdd = { 
    "product": { 
    "id": "123456789", 
    "sku_code": "135792468" 
    }, 
    "quantity": 3, 
    "subtotal": 369, 
    "total_discount": 0 
}; 
obj["line_items"].push(productToAdd); 

當我們增加我們所有的產品對象,我們可以JSON.stringify()如果我們需要發送的對象爲JSON回服務器:

var jsonString = JSON.stringify(obj); 

如果你有一個實際的JSON字符串在你的到來可以使用JSON.parse()來像

var obj = JSON.parse('{"obj":{"param":"val"}}'); 

var json = '{"obj":{"param":"val"}}'; 
var obj = JSON.parse(json); 

然後添加對象。

+0

我希望這是命令有點權。我覺得應該以不同的順序解釋,以避免多餘的解釋。 – DutGRIFF

+0

假設他提供了一個片段,我正在從網站的實際對象出發 - 儘管這是可靠的信息。 –

2

一旦你的數據 - 它應該是相當簡單(基於上述網站的數據) -

window.universal_variable["basket"]["line_items"].push(myProductObj); 

myProductObj是以下幾點:

{ 
     "product": { 
     "id": "1234567890", 
     "sku_code": "0987654321", 
     "url": "product.html", 
     "name": "Sparkly Shoes", 
     "description": "Description about this product", 
     "manufacturer": "The Shoe Co", 
     "category": "Shoe", 
     "subcategory": "Heels", 
     "color": "n/a", 
     "stock": 3, 
     "size": "6", 
     "currency": "GBP", 
     "unit_price": 130, 
     "unit_sale_price": 130, 
     "voucher": "MYVOUCHER1" 
     }, 
     "quantity": 1, 
     "subtotal": 130, 
     "total_discount": 0 
    } 
相關問題