簡體   English   中英

更改 JSON 格式/布局

[英]Change JSON format/layout

我的網站上有一個通用變量,其中包括具有相關詳細信息的行項目。 這些訂單項反映了用戶購物車中的商品。 我正在與第三方集成,第三方要求傳遞給他們的數據的格式略有不同。 以下是我網站上當前的數據層:

"lineItems": [
    {
        "product": {
            "id": "s83y016b5",
            "sku_code": "s83y016b5",
            "url": "/en-gb/jeans/p/s83y016b5",
            "image_url": "http://www.my-website.com/a/p/shirt.jpeg",
            "name": "Jeans",
            "manufacturer": "",
            "category": "Everyday Wear",
            "stock": 116,
            "currency": "GBP",
            "unit_sale_price": 16,
            "unit_price": 16,
            "size": "6-9 Months",
            "color": "Indigo"
        },
        "quantity": 1
    }
]

以下是第三方需要的格式:

"lineItems": [
  {
    "sku": "s83y016b5",
    "name": "Jeans",
    "description": "A super great pair of jeans.",
    "category": "Everyday Wear",
    "other": {"fieldName": "This can be a string or any value you like"}
    "unitPrice": 11.99,
    "salePrice": 11.99,
    "quantity": 2,
    "totalPrice": 23.98
    "imageUrl": "http://www.my-website.com/a/p/shirt.jpeg",
    "productUrl": "http://www.my-website.com/index.php/shirt.html",
  }]

顯然,這需要基於購物車中的產品是動態的。 我打算做的是使用javascript修改數據並通過Google Tag Manager將其發送給第三方。

任何幫助將不勝感激。 歡迎任何問題。

這應該接近你正在尋找的。

let oldLineItems = "your object"; 

let newLineItems = {};
newLineItems.lineItems = [];

for (let i in oldLineItems.lineItems) {
    newLineItems.lineItems[i] = {};
    for (let key in oldLineItems.lineItems[i].product)
    {
        newLineItems.lineItems[i][key] = oldLineItems.lineItems[i].product[key];
    }
}

請參閱下面的代碼。 我不確定您的 lineItems 對象是如何設置的,但在下面我剛剛創建了一個名為 line Items 的數組。 如果行項目是我從上面的代碼片段中懷疑的對象中的一個鍵,則您必須在下面的示例中使用的 for 循環中更深入地研究。

只需在下面的嵌套 for in 循環中向新對象添加更多詳細信息。

 var lineItems = [ { "product": { "id": "s83y016b5", "sku_code": "s83y016b5", "url": "/en-gb/jeans/p/s83y016b5", "image_url": "http://www.my-website.com/a/p/shirt.jpeg", "name": "Jeans", "manufacturer": "", "category": "Everyday Wear", "stock": 116, "currency": "GBP", "unit_sale_price": 16, "unit_price": 16, "size": "6-9 Months", "color": "Indigo", "description": 'Some random description' }, "quantity": 1 }, { "product": { "id": "s83y01699", "sku_code": "s83y01699", "url": "/en-gb/pants/p/s83y016b5", "image_url": "http://www.my-website.com/a/p/pants.jpeg", "name": "Pants", "manufacturer": "", "category": "Casual Wear", "stock": 90, "currency": "au", "unit_sale_price": 14, "unit_price": 14, "size": "6-9 Months", "color": "Indigo", "description": 'Some random description' }, "quantity": 14 }, ]; var newLineItems = []; for(var char in lineItems){ // Adding some values to newLineItems. newLineItems.push({ sku: lineItems[char].product.sku_code, name: lineItems[char].product.name, description: lineItems[char].product.description, category: lineItems[char].product.category, quantity: lineItems[char].quantity }); } console.log(JSON.stringify(newLineItems));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM