[英]Mule ESB: Converting JSON Object to another object
我有以下流程:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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" version="EE-3.5.0"
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/db http://www.mulesoft.org/schema/mule/db/current/mule-db.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/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
<db:generic-config name="Generic_Database_Configuration" url="jdbc:db2://localhost:50000/TEST:user=instuid;password=instpw;" driverClassName="com.ibm.db2.jcc.DB2Driver" doc:name="Generic Database Configuration"/>
<flow name="test2Flow1" doc:name="test2Flow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
<db:select config-ref="Generic_Database_Configuration" doc:name="Database" doc:description="test">
<db:parameterized-query><![CDATA[SELECT SUM(BAL) FROM xxxx.ACCT]]></db:parameterized-query>
</db:select>
<json:object-to-json-transformer doc:name="Object to JSON"/>
</flow>
</mule>
流詞很好,並且您可以看到JSON對象將Database對象轉換為以下內容:(帶有單個對象的JSON arry):
[{"1":444}]
請注意,444是一個數字值(它周圍沒有引號)。
我想做的事
創建一個沒有數組的簡單JSON結構
將444從數字值更改為字符串值
使它看起來像:(將444放置到另一個結構中)
{“總計”:“ 444”,“日期”:“ 14/07/14”}
我知道要獲取系統日期,請執行以下操作:
#[server.dateTime.format('dd/MM/yy')]
...並且我知道要從原始字符串中獲取444值,我執行了以下操作:
$..1
但是我不知道下一步該怎么做。
既然我已經使用JSON對象查看數據庫連接器的結果,接下來要做什么對象來創建新結構。
我是否使用另一個JSON對象,將如何構造表達式?
要將所有數字寫為字符串,可以在Jackson對象映射器上進行設置,並從轉換器中引用該自定義對象映射器:
<spring:beans>
<spring:bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />
<spring:bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<spring:property name="targetObject" ref="jacksonObjectMapper" />
<spring:property name="targetMethod" value="configure" />
<spring:property name="arguments">
<spring:list>
<spring:value>WRITE_NUMBERS_AS_STRINGS</spring:value>
<spring:value>true</spring:value>
</spring:list>
</spring:property>
</spring:bean>
</spring:beans>
<flow name="flow1" doc:name="flow1">
...
<json:object-to-json-transformer mapper-ref="jacksonObjectMapper" />
...
</flow>
但是,這將在所有數字字段中將其加點。 您可能必須針對特定行為編寫自定義序列化程序。
至於展開數組。 不確定是否可以通過具有jackson版本的ule子使用的對象映射器來執行此操作。 但是對於這種情況,如果您始終只是從查詢中獲得一個結果-您可能只需將數組展開到json轉換器之前
<set-payload value="#[payload[0]]">
<json:object-to-json-transformer mapper-ref="jacksonObjectMapper" />
一種簡單的方法是使用Mule的Expression Transformer 。
您需要從JSON數組中提取值並將其存儲到如下所示的一些變量中:
<json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to Object" />
<set-variable variableName="Total" value="#[message.payload[0].1]" doc:name="Variable" />
現在,您的變量Total將包含值444
下一步是將Date存儲到其他變量中,如下所示:
<set-variable variableName="Date" value="#[server.dateTime.format('dd/MM/yy')]" doc:name="Variable" />
現在,如果完成了這兩個步驟,則可以使用Expression Transformer以非常簡單的方式創建所需的JSON ,如下所示:
<expression-transformer
expression="#[[
'Total': flowVars['Total'].toString(),
'Date': flowVars['Date']
]]" doc:name="Expression" />
<json:object-to-json-transformer doc:name="Object to JSON" />
這將以非常簡單的方式生成並構造所需的JSON:- {"Date":"12/08/15","Total":"444"}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.