我正在使用Shopify,必须将Qubit添加到客户站点。
在任何时候,都有一个“购物篮”对象,它显示用户购物车中的产品。
如何动态地将对象插入到这个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
可能是一个产品,或者两个产品,等等.如何动态地迭代购物车中的可用产品(psuedo代码很好),然后显示一些值?
如果我定义了上面的json结构,我将如何动态地完成它?在代码之间放一个循环?(感觉很脏?)
发布于 2014-01-15 02:45:39
您提供的代码不是JSON。它是一个javascript对象,缺少一个{,因为它没有声明。您可以使用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
}]
};现在我们可以使用Kelly的解决方案并将其添加到对象中:
var productToAdd = {
"product": {
"id": "123456789",
"sku_code": "135792468"
},
"quantity": 3,
"subtotal": 369,
"total_discount": 0
};
obj["line_items"].push(productToAdd);当我们将所有产品添加到对象中时,如果需要将对象作为JSON发送回服务器,则可以使用JSON.stringify():
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);然后添加对象。
发布于 2014-01-15 01:53:04
一旦你有了数据-它应该相当简单(基于上述网站的数据)-
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
}https://stackoverflow.com/questions/21127546
复制相似问题