【问题标题】:Convert string to boolean Mule4 Dataweave将字符串转换为布尔型 Mule4 Dataweave
【发布时间】:2020-09-06 01:46:57
【问题描述】:

我有 mule4 DW 表达式,我想将其转换为布尔值“作为布尔值”无法正常工作,或者我只是放错了位置。

DW 表达式:

%dw 2.0
output application/java
ns ns0 http://www.demandware.com/xml/impex/order/2006-10-31
---
[{
    Id: (payload.ns0#order.ns0#"custom-attributes".*ns0#"custom-attribute" filter(item) -> (item.@"attribute-id" == "sscAccountid")) [0],
    Marketing_Opt_in__c: (payload.ns0#order.ns0#"custom-attributes".*ns0#"custom-attribute" filter(item) -> (item.@"attribute-id" == "newsLetterRegistration")) [0] //***This output I want to convert into Boolean***
    
}]

XML 标记

<custom-attribute attribute-id="newsLetterRegistration">false</custom-attribute>

【问题讨论】:

  • 嗨,Pankaj。 (payload.ns0#order.ns0#"custom-attributes".*ns0#"custom-attribute" filter(item) -&gt; (item.@"attribute-id" == "newsLetterRegistration")) [0]return 的表达式是什么?诸如“真”或“假”之类的东西。我没有得到需要转换为布尔值的内容
  • 是的,它返回真/假,我已经用正在提取的 XML 标记更新了问题
  • 您还需要共享完整的 XML。否则我们无法知道可能是什么错误。同时分享你遇到的错误。

标签: dataweave mulesoft mule4


【解决方案1】:
[{
Id: (payload.order."custom-attributes".*"custom-attribute" filter(item) -> (item.@"attribute-id" == "sscAccountid"))[0],
Marketing_Opt_in__c: (payload.order."custom-attributes".*"custom-attribute" filter(item) -> (item.@"attribute-id" == "newsLetterRegistration"))[0]
 ~= 'True'  //***This output I want to convert into Boolean***
}]

为了使它不区分大小写,将比较的两边都转换为一个大小写,比如大写

upper((item.@"attribute-id" == "newsLetterRegistration"))[0])
     ~= 'TRUE' 

另一种方法是使用自动执行此转换的 Java 布尔类。

(item.@"attribute-id" == "newsLetterRegistration"))[0] as Boolean
 

【讨论】:

  • 谢谢亚历克斯,这几乎可以工作,但即使我通过 true,它也总是返回“false”,你能检查一下吗?
  • 从 'True' 中删除单引号后它工作了,非常感谢
猜你喜欢
  • 2022-11-18
  • 2018-09-07
  • 1970-01-01
  • 2014-05-11
  • 2012-03-10
  • 2015-02-23
  • 2012-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多