如何根据提供的国家为每个字段动态制作这些货币符号

问题描述 投票:0回答:1

有了这个静态有效负载,我将如何根据请求中提供的国家更改这些货币符号

[
    {
        "orderId": "11111101",
        "details": {
            "TotalPrice": {
                "NetPrice__c": 11.11,
                "NetPriceFormatted__c": "11,11 $"
            },
            "TotalCartValue__c": 11.11,
            "TotalCartValueFormatted__c": "11,11 $",
            "TotalTaxPrice__c": 11.11,
            "TotalTaxPriceFormatted__c": "11,11 $",
            "TotalDiscountPrice__c": 11.11,
            "TotalDiscountPriceFormatted__c": "11,11 $",
            "products": [
                {
                    "SKUNumber__c": "111111-01",
                    "Quantity": "1",
                    "UnitPrice": 11.11,
                    "UnitPriceFormatted__c": "11,11 $",
                    "TotalPrice": 11.11,
                    "TotalPriceFormatted__c": "11,11 $",
                    "Discount__c": 11.11,
                    "DiscountFormatted__c": "11,11 $",
                    "ProductName__c": "Apple",
                    "MachineFriendlyName__c": "iPhone",
                    "DiscountValue__c": 11.11,
                    "bundles": []
                }
            ],
             "shippingModes": [
                "ClickAndCollect",
                "ScheduledDelivery",
                "StandardDelivery"
            ],
            "TotalItems__c": 1,
            "PromoCode__c": null,
            "PromoStatus__c": null
        }
    },
    {
        "orderId": "22222202",
        "details": {
            "TotalPrice": {
                "NetPrice__c": 22.22,
                "NetPriceFormatted__c": "22,22 $"
            },
            "TotalCartValue__c": 22.22,
            "TotalCartValueFormatted__c": "22,22 $",
            "TotalTaxPrice__c": 22.22,
            "TotalTaxPriceFormatted__c": "22,22 $",
            "TotalDiscountPrice__c": 22.22,
            "TotalDiscountPriceFormatted__c": "22,22 $",
            "products": [
                {
                    "SKUNumber__c": "222222-02",
                    "Quantity": "1",
                    "UnitPrice": 22.22,
                    "UnitPriceFormatted__c": "22,22 $",
                    "TotalPrice": 22.22,
                    "TotalPriceFormatted__c": "22,22 $",
                    "Discount__c": 22.22,
                    "DiscountFormatted__c": "22,22 $",
                    "ProductName__c": "Samsung",
                    "MachineFriendlyName__c": "Galaxy",
                    "DiscountValue__c": 22.22,
                    "bundles": []
                }
            ],
             "shippingModes": [
                "ClickAndCollect",
                "ScheduledDelivery",
                "StandardDelivery"
            ],
            "TotalItems__c": 1,
            "PromoCode__c": null,
            "PromoStatus__c": null
        }
    }
]

例如请求中的国家是意大利,用orderId为11111101过滤

结果将是:(静态负载结构相同,唯一不同的是货币符号会改变)

[
    {
        "orderId": "11111101",
        "details": {
            "TotalPrice": {
                "NetPrice__c": 11.11,
                "NetPriceFormatted__c": "11,11 €"
            },
            "TotalCartValue__c": 11.11,
            "TotalCartValueFormatted__c": "11,11 €",
            "TotalTaxPrice__c": 11.11,
            "TotalTaxPriceFormatted__c": "11,11 €",
            "TotalDiscountPrice__c": 11.11,
            "TotalDiscountPriceFormatted__c": "11,11 €",
            "products": [
                {
                    "SKUNumber__c": "111111-01",
                    "Quantity": "1",
                    "UnitPrice": 11.11,
                    "UnitPriceFormatted__c": "11,11 €",
                    "TotalPrice": 11.11,
                    "TotalPriceFormatted__c": "11,11 €",
                    "Discount__c": 11.11,
                    "DiscountFormatted__c": "11,11 €",
                    "ProductName__c": "Apple",
                    "MachineFriendlyName__c": "iPhone",
                    "DiscountValue__c": 11.11,
                    "bundles": []
                }
            ],
             "shippingModes": [
                "ClickAndCollect",
                "ScheduledDelivery",
                "StandardDelivery"
            ],
            "TotalItems__c": 1,
            "PromoCode__c": null,
            "PromoStatus__c": null
        }
    }
]```

dataweave mulesoft
1个回答
0
投票

您可以看到这个递归解决方案,它根据 countryNames 变量上存在的值更新每个字段,并将其替换为您需要的符号。您可以在此处参考有关replace的更多信息

数据编织

%dw 2.0
output application/json
var countryNames = {
    "England": "£",
    "Philliphines": "₱"
}
fun replaceDollarWithCountryValue(data: Any, country: String): Any =
    data match {
        case is Array -> data map replaceDollarWithCountryValue($, country)
        case is Object -> $ mapObject ((value, key, index) ->
            (key): replaceDollarWithCountryValue(value, country))
        
        else -> if (($ is String)and($ contains "\$"))
                    $ replace "\$" with countryNames[country] 
                else $
    }
---
replaceDollarWithCountryValue(payload, "England")

输出

[
  {
    "orderId": "11111101",
    "details": {
      "TotalPrice": {
        "NetPrice__c": 11.11,
        "NetPriceFormatted__c": "11,11 £"
      },
      "TotalCartValue__c": 11.11,
      "TotalCartValueFormatted__c": "11,11 £",
      "TotalTaxPrice__c": 11.11,
      "TotalTaxPriceFormatted__c": "11,11 £",
      "TotalDiscountPrice__c": 11.11,
      "TotalDiscountPriceFormatted__c": "11,11 £",
      "products": [
        {
          "SKUNumber__c": "111111-01",
          "Quantity": "1",
          "UnitPrice": 11.11,
          "UnitPriceFormatted__c": "11,11 £",
          "TotalPrice": 11.11,
          "TotalPriceFormatted__c": "11,11 £",
          "Discount__c": 11.11,
          "DiscountFormatted__c": "11,11 £",
          "ProductName__c": "Apple",
          "MachineFriendlyName__c": "iPhone",
          "DiscountValue__c": 11.11,
          "bundles": [
            
          ]
        }
      ],
      "shippingModes": [
        "ClickAndCollect",
        "ScheduledDelivery",
        "StandardDelivery"
      ],
      "TotalItems__c": 1,
      "PromoCode__c": null,
      "PromoStatus__c": null
    }
  },
  {
    "orderId": "22222202",
    "details": {
      "TotalPrice": {
        "NetPrice__c": 22.22,
        "NetPriceFormatted__c": "22,22 £"
      },
      "TotalCartValue__c": 22.22,
      "TotalCartValueFormatted__c": "22,22 £",
      "TotalTaxPrice__c": 22.22,
      "TotalTaxPriceFormatted__c": "22,22 £",
      "TotalDiscountPrice__c": 22.22,
      "TotalDiscountPriceFormatted__c": "22,22 £",
      "products": [
        {
          "SKUNumber__c": "222222-02",
          "Quantity": "1",
          "UnitPrice": 22.22,
          "UnitPriceFormatted__c": "22,22 £",
          "TotalPrice": 22.22,
          "TotalPriceFormatted__c": "22,22 £",
          "Discount__c": 22.22,
          "DiscountFormatted__c": "22,22 £",
          "ProductName__c": "Samsung",
          "MachineFriendlyName__c": "Galaxy",
          "DiscountValue__c": 22.22,
          "bundles": [
            
          ]
        }
      ],
      "shippingModes": [
        "ClickAndCollect",
        "ScheduledDelivery",
        "StandardDelivery"
      ],
      "TotalItems__c": 1,
      "PromoCode__c": null,
      "PromoStatus__c": null
    }
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.