2014-01-15 6 views
0

Shopify를 사용 중이며 고객 사이트에 Qubit을 추가해야합니다.프로그래밍 방식으로 JS 객체에 추가하는 방법은 무엇입니까?

언제든지 사용자 바구니에 어떤 제품이 있는지 보여주는 "바구니"개체가 있습니다.

이 json 구조에 개체를 어떻게 동적으로 삽입합니까? 어떻게 동적으로 (사이비 코드는 괜찮) 후 장바구니에서 사용할 수있는 제품을 통해 반복 것 ... 그것은 등 하나 개의 제품, 또는 두 개의 제품을 수 http://tools.qubitproducts.com/uv/demosite/guide.html

:

"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 
    }] 
    }, 

은에서 촬영 몇 가지 값을 표시 하시겠습니까?


위의 json 구조를 어떻게 정의 할 수 있습니까? 코드 사이에 루프를 넣으시겠습니까? (더러운 느낌?)

+0

JSON (직렬화 형식)을 Javascript 객체 (조작 할 수있는 런타임 객체)와 혼동스럽게 생각할 수 있습니다. 어떤 종류의 코드를 생각하고 있는지 몇 가지 예를 들려 줄 수 있습니까? – hugomg

+0

이미 제품 데이터 필드를 가져온 후 장바구니에 제품을 추가하는 방법을 묻는 중입니까? –

답변

1

제공하신 코드는 JSON이 아닙니다. 그것은 누락되었습니다 명시되지 않은 { 누락 된 자바 스크립트 개체입니다. Kelly J Andrews 대답을 사용하여 개체에 항목을 추가하거나 개체를 복사하고 항목을 추가하여 업데이트 된 개체를 만들 수 있습니다. 당신이 경우에 당신은 이미 개체가 JSON.stringify()

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

또는

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

그러나 사용할 수있는 JSON에 객체를 얻으려면. 당신이 제공하는 코드 줄 바꿈을 가지고 있으며, 그래서 우리는 쉽게 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); 
,536,913,632 10

당신은 실제 JSON 문자열이 다음 개체를 추가

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

또는

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

등) (JSON.parse를 사용 할 수 있습니다 오는 경우.

+0

나는 이것이 다소 옳았다는 것을 희망한다. 나는 그것이 중복 된 설명을 피하기 위해 다른 순서로 설명되어야한다고 생각한다. – DutGRIFF

+0

나는 웹 사이트에서 실제 객체를 보려고했는데, 그가 스 니펫 (snippet)을 제공했다는 가정하에 - 이것은 단단한 정보입니다. –

2

당신은 데이터 일단 -이 (위에서 언급 한 사이트의 데이터를 기준으로) 매우 간단해야한다 - myProductObj은 다음과 같은 것이다

window.universal_variable["basket"]["line_items"].push(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 
    } 
관련 문제