我正在尝试将Java Map对象添加到JSON列表项
Java Map
{PARAMROWKEYVALUE3=PARAMROWVALUEVAL3, PARAMROWKEYVALUE4=PARAMROWVALUEVAL4}
到下面的JSON的OBJECT2地图
{
"root": {
"OBJECT1": {
"PARAM1": "PARAM1VALUE",
"PARAM2": "PARAM2VALUE"
},
"OBJECT2": [{
"KEY": "PARAMROWKEYVALUE1",
"VALUE": "PARAMROWVALUEVAL1"
}, {
"KEY": "PARAMROWKEYVALUE2",
"VALUE": "PARAMROWVALUEVAL2"
}],
"OBJECT3": {
"PARAM3": "PARAM3VALUE",
"PARAM4": "PARAM4VALUE"
}
}
}
请让我知道如何实现这一点,将Map作为流量变量。
提前致谢。
答案 0 :(得分:0)
我想我有你的答案,但不仅仅是DataWeave。 由于您想在另一个地图中添加地图,因此组件将是最佳选择。但DataWeave可以很方便地转换flowVar。 要完全在DataWeave上执行此操作,您需要重新映射所有整个对象,就像重新创建它一样,没有多大意义。 看看下面的Mule配置,让我知道这是否是你想要的。
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata"
xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<http:listener-config name="HTTP_Listener_Configuration"
host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration" />
<flow name="stackoverflowFlow">
<http:listener config-ref="HTTP_Listener_Configuration"
path="/" doc:name="HTTP" />
<set-variable variableName="map"
value="#[[["PARAMROWKEYVALUE3":"PARAMROWVALUEVAL3"], ["PARAMROWKEYVALUE4":"PARAMROWVALUEVAL4"]]]"
doc:name="Variable" />
<dw:transform-message metadata:id="283a0aba-e3dc-4d86-8cdf-0a90afd259f5"
doc:name="Transform Message">
<dw:input-variable doc:sample="list_UserDefined.dwl"
variableName="map" />
<dw:set-variable variableName="map"><![CDATA[%dw 1.0
%output application/java
---
flowVars["map"] map
{
"KEY": ($ pluck $$ as :string)[0],
"VALUE": ($ pluck $ as :string)[0]
}]]></dw:set-variable>
</dw:transform-message>
<set-payload
value="{"root": { "OBJECT1": { "PARAM1": "PARAM1VALUE", "PARAM2": "PARAM2VALUE" }, "OBJECT2": [{ "KEY": "PARAMROWKEYVALUE1", "VALUE": "PARAMROWVALUEVAL1" }, { "KEY": "PARAMROWKEYVALUE2", "VALUE": "PARAMROWVALUEVAL2" }], "OBJECT3": { "PARAM3": "PARAM3VALUE", "PARAM4": "PARAM4VALUE" }}}"
encoding="UTF-8" mimeType="text/json" doc:name="Set Payload" />
<json:json-to-object-transformer
returnClass="java.util.Map" encoding="UTF-8" mimeType="text/json"
doc:name="JSON to Object" />
<expression-component doc:name="Expression"><![CDATA[payload.root.OBJECT2.addAll(flowVars.map)]]></expression-component>
<object-to-string-transformer doc:name="Object to String" />
</flow>
</mule>