将 JSONPath 值与 Mule ESB 中的 flowVars 值进行比较
Compare JSONPath value with flowVars value in Mule ESB
我需要将下面 JSON 中的用户名与 Mule ESB 3.8.3 中的 flowVars 进行比较
{"id":"users_0001","username":"0001","firstName":"AB","lastName":"C","email":"abc@abc.com","enabled":true}
在选择运算符中使用此表达式
<choice doc:name="Choice">
<when expression="#[json:[0]/username != flowVars.username]">
<flow-ref name="put account" doc:name="put account"/>
</when>
<otherwise>
<flow-ref name="do nothing" doc:name="do nothing"/>
</otherwise>
</choice>
在调试过程中,我可以看到 json:[0]/username 和 flowVars.username 都是 returning 相同的值,但是为什么在比较它们时总是 return假的?
这是我评估它们的结果
flowVars.username == "0001", returns true
flowVars.username == '0001', returns true
flowVars.username == 0001, returns true
json:[0]/username = 0001, returns true
json:[0]/username = "0001", returns false
json:[0]/username = '0001', returns false
json:[0]/username != flowVars.username, returns true
json:[0]/username = flowVars.username, returns false
我会避免使用#[json] 工具,只是将传入的 JSON 转换为 HashMap。你的生活会更轻松,而且使用 HashMap 也更容易。
根据文档:https://docs.mulesoft.com/mule-user-guide/v/3.7/mule-expression-language-tips
JSON 处理中
MEL 不直接支持 JSON。 json-to-object-transformer 可以将 JSON 有效负载转换为简单数据结构的层次结构,这些结构很容易用 MEL 进行解析。
在你的情况下,是这样的:
<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
<choice doc:name="Choice">
<when expression="#[payload.username != flowVars.username]">
<flow-ref name="put account" doc:name="put account"/>
</when>
<otherwise>
<flow-ref name="do nothing" doc:name="do nothing"/>
</otherwise>
</choice>
我需要将下面 JSON 中的用户名与 Mule ESB 3.8.3 中的 flowVars 进行比较
{"id":"users_0001","username":"0001","firstName":"AB","lastName":"C","email":"abc@abc.com","enabled":true}
在选择运算符中使用此表达式
<choice doc:name="Choice">
<when expression="#[json:[0]/username != flowVars.username]">
<flow-ref name="put account" doc:name="put account"/>
</when>
<otherwise>
<flow-ref name="do nothing" doc:name="do nothing"/>
</otherwise>
</choice>
在调试过程中,我可以看到 json:[0]/username 和 flowVars.username 都是 returning 相同的值,但是为什么在比较它们时总是 return假的?
这是我评估它们的结果
flowVars.username == "0001", returns true
flowVars.username == '0001', returns true
flowVars.username == 0001, returns true
json:[0]/username = 0001, returns true
json:[0]/username = "0001", returns false
json:[0]/username = '0001', returns false
json:[0]/username != flowVars.username, returns true
json:[0]/username = flowVars.username, returns false
我会避免使用#[json] 工具,只是将传入的 JSON 转换为 HashMap。你的生活会更轻松,而且使用 HashMap 也更容易。
根据文档:https://docs.mulesoft.com/mule-user-guide/v/3.7/mule-expression-language-tips
JSON 处理中 MEL 不直接支持 JSON。 json-to-object-transformer 可以将 JSON 有效负载转换为简单数据结构的层次结构,这些结构很容易用 MEL 进行解析。
在你的情况下,是这样的:
<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
<choice doc:name="Choice">
<when expression="#[payload.username != flowVars.username]">
<flow-ref name="put account" doc:name="put account"/>
</when>
<otherwise>
<flow-ref name="do nothing" doc:name="do nothing"/>
</otherwise>
</choice>