2

我正在尝试将 Java Map 对象添加到 JSON 列表项

爪哇地图

{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 作为流变量。

提前致谢。

4

1 回答 1

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="#[[[&quot;PARAMROWKEYVALUE3&quot;:&quot;PARAMROWVALUEVAL3&quot;], [&quot;PARAMROWKEYVALUE4&quot;:&quot;PARAMROWVALUEVAL4&quot;]]]"
            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="{&quot;root&quot;: {    &quot;OBJECT1&quot;: {        &quot;PARAM1&quot;: &quot;PARAM1VALUE&quot;,        &quot;PARAM2&quot;: &quot;PARAM2VALUE&quot;    },    &quot;OBJECT2&quot;: [{        &quot;KEY&quot;: &quot;PARAMROWKEYVALUE1&quot;,        &quot;VALUE&quot;: &quot;PARAMROWVALUEVAL1&quot;    }, {        &quot;KEY&quot;: &quot;PARAMROWKEYVALUE2&quot;,        &quot;VALUE&quot;: &quot;PARAMROWVALUEVAL2&quot;    }],    &quot;OBJECT3&quot;: {        &quot;PARAM3&quot;: &quot;PARAM3VALUE&quot;,        &quot;PARAM4&quot;: &quot;PARAM4VALUE&quot;    }}}"
            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>
于 2016-03-06T23:51:34.363 回答