我有一个类似的JSON:
{
"EDITORS" : [{
"EDITOR" : "MCGRAM HILL",
"BOOKS" : [{
"NAME" : "DIFFERENTIAL CALCULUS",
"YEAR" : "1995",
"TIMES_READ" : "135"
}, {
"NAME" : "2012 THE END OF THE WORLD",
"YEAR" : "2012,",
"TIMES_READ" : "56"
}
]
}, {
"EDITOR" : "DEMIDOVICH",
"BOOKS" : [{
"NAME" : "SOME TITTLE",
"YEAR" : "1975,",
"TIMES_READ" : "154"
}, {
"NAME" : "THE LITTLE PRINCE",
"YEAR" : "1987,",
"TIMES_READ" : "57"
}
]
}, {
"EDITOR" : "ADRIAN LOPEZ ASC.",
"BOOKS" : [{
"NAME" : "SOMETHING",
"YEAR" : "2008,",
"TIMES_READ" : "10"
}
]
}
]
}
我需要将其转换为输出,如:
payload[0]= The editor MCGRAM HILL has:
The book DIFFERENTIAL CALCULUS published in 1995 has been readed 135 times.
The book 2012 THE END OF THE WORLD published in 2012 has been readed 56 times.
payload[1]= The editor DEMIDOVICH has:
The book SOME TITTLE published in 1975 has been readed 154 times.
The book THE LITTLE PRINCE published in 1987 has been readed 57 times.
payload[2]= The editor ADRIAN LOPEZ ASC. has:
The book SOMETHING published in 2008 has been readed 10 times.
到目前为止,我将JSON转换为Java对象,然后将foreach范围与集合#[payload.EDITORS]一起使用 在foreach中我创建了一个变量Intro ="编辑#[payload.EDITOR]有:" 之后,我迷失了。 我曾尝试将有效负载设置为#[payload.BOOKS],然后使用另一个foreach,但这只显示了最后一本书,并且还尝试了Collection Aggregator但不知道该写什么"消息信息映射& #34;
如何完成所需的输出?
非常感谢任何帮助,提前谢谢。
答案 0 :(得分:0)
恕我直言,您不需要在 foreach 范围内设置有效负载。
我尝试了以下配置,它会生成一个包含这些消息的字符串列表。我认为您可以修改最后的消息处理器以显示预期的输出(例如:添加另一个 foreach 或表达式以从最终提取消息有效载荷/ ArrayList的)。
<json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>
<set-variable variableName="messageMapping" value="#[new java.util.ArrayList()]" doc:name="Variable"/>
<foreach collection="#[payload.EDITORS]" doc:name="For Each">
<set-variable variableName="messageEditor" value="The editor #[payload.EDITOR] has: " doc:name="Variable"/>
<foreach collection="#[payload.BOOKS]" doc:name="For Each">
<expression-transformer expression="#[flowVars.messageEditor += "\r\nThe book " + payload.NAME + " published in " + payload.YEAR + " has been readed " + payload.TIMES_READ + " times."]" doc:name="Expression"/>
</foreach>
<expression-transformer expression="#[flowVars.messageMapping.add(flowVars.messageEditor)]" doc:name="Expression"/>
</foreach>
<set-payload value="#[flowVars.messageMapping]" doc:name="Set Payload"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
如果您更喜欢使用DataWeave,则可以使用以下代码:
%dw 1.0
%output application/java
---
payload.EDITORS map (
'The editor ' ++ $.EDITOR ++ ' has:\r\n' ++
($.BOOKS map (
'The book ' ++ $.NAME ++ ' published in ' ++ $.YEAR ++ ' has been readed ' ++ $.TIMES_READ ++ ' times.'
) joinBy '\r\n')
)
答案 1 :(得分:0)
尝试使用dataweave
%dw 1.0
%output application/json
---
{(payload.EDITORS map ((data, index) -> {
payload : "The editor " ++ data.EDITOR ++ " has:\r\n" ++
{(data.BOOKS map {
pqr : "The book " ++ $.NAME ++ " published in " ++ $.YEAR ++ " has been readed " ++ $.'TIMES_READ' ++ " times."
})} pluck $ joinBy "\r\n"
}))} pluck $
Json输出以供参考。您可以转换为java以获得所需的输出。
[
"The editor MCGRAM HILL has:\r\nThe book DIFFERENTIAL CALCULUS published in 1995 has been readed 135 times.\r\nThe book 2012 THE END OF THE WORLD published in 2012, has been readed 56 times.",
"The editor DEMIDOVICH has:\r\nThe book SOME TITTLE published in 1975, has been readed 154 times.\r\nThe book THE LITTLE PRINCE published in 1987, has been readed 57 times.",
"The editor ADRIAN LOPEZ ASC. has:\r\nThe book SOMETHING published in 2008, has been readed 10 times."
]
希望这有帮助。
答案 2 :(得分:0)
Dataweave doens感觉不是正确的方法,特别是因为输出是纯文本。
我个人可能会使用json-to-object(见下文)和parse template。
<json:json-to-object-transformer returnClass="java.util.Map" />