Mulesoft DataWeave:如何为带引号的字符串转义 $

age*_*ntv 1 dataweave mulesoft

...我正在使用 Anypoint Studio 7.6 并在 DataWeave 2.0 中编写

我找不到在带引号的字符串中包含 $(美元符号)字符的明确方法。

这是我尝试过的:

%dw 2.0
output application/dw
var sign = "\u0024" // unicode dollar sign
type currency = String {format: "$sign ###.00"} // interpolation from previous var
var cost = 100 as currency
---
{
    directly: "a dollar sign like this: \$",
    asdefined: sign,
    indirectly: "This flight, costs $(cost), and is operated by " 
       ++ payload.airline
}
Run Code Online (Sandbox Code Playgroud)

这是我遇到的麻烦:

{
  directly: "a dollar sign like this: \$",
  asdefined: "\$",
  indirectly: "This flight, costs \$ 100.00, and is operated by United"
}
Run Code Online (Sandbox Code Playgroud)

我觉得我错过了一些简单的东西。

mac*_*val 6

嗨,您所看到的没有问题,只是您使用 application/dw 作为输出。在那种格式中,$需要转义这就是为什么你会这样看。如果您更改为application/json它们将消失。

  • 仅供记录,application/dw 应该仅用于故障排除,而不是作为“标准”格式。它对性能和兼容性有影响。默认情况下使用它有忘记它存在的风险,而且它看起来与 JSON 非常相似,因此并不明显。 (2认同)