2017-03-10 116 views
1

想要从我的json有效内容中提取每个对象,如下所示:需要将每个对象从此映射传递到splitter或collection splitter。我不能在这里使用foreach范围。Mule表达式从Json数组中获取单个对象

[ 
    { 
     "Name": null, 
     "Key": "4", 
     "Header": { 
     "Id": "14" 
     } 
    }, 
    { 
     "Name": null, 
     "Key": "5", 
     "Header": { 
     "Id": "15" 
     } 
    } 
] 

回答

0

要从数组中获得整个对象,我们可以使用简单的[[有效载荷]],这将像每个作用域一样小心。

0

如果你想解析和访问JSON元素,然后 **<json:json-to-object-transformer/>**是做的伎俩。 java.util.HashMap or java.util.List or java.util.Map 取决于您的JSON数据的类型。 也可以使用java.lang.Object

+0

您还可以使用#[JSON:数据/场] –

+0

我会使用建议不'#[json:data/field]'表达式。 JsonPath表达式评估器已被弃用。更多信息。可以在这里找到:https://docs.mulesoft.com/mule-user-guide/v/3.8/json-module-reference。 –

+0

我仍然收到异常。即使我使用java.util.List或java.lang.Object。 org.mule.api.transformer.TransformerMessagingException:转换的对象类型为:“SimpleDataType {type = java.lang.String,mimeType ='*/*',encoding ='null'}”,但预期的返回类型是“SimpleDataType {type = java.util.List,mimeType ='application/json',encoding ='null'}”。 (org.mule.api.transformer.TransformerMessagingException) – user4338724

0

有多种方式来做到这一点: 1)作为由维杰提到的,使用JSON到对象的变压器输入JSON映射到任何集合类型例如

<json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to Object"/> 
    <collection-splitter doc:name="Collection Splitter"/> 

2)使用dataweave从输入JSON数据得到一个集合:

<dw:transform-message doc:name="Transform Message"> 
      <dw:set-payload><![CDATA[%dw 1.0 
%output application/java 
--- 
payload 
]]></dw:set-payload> 
     </dw:transform-message> 
<collection-splitter doc:name="Collection Splitter"/> 

+0

Getting the下面以异常的形式使用dataweave脚本时提示Root异常堆栈跟踪: java.lang.IllegalArgumentException:对象“com.mulesoft.weave.reader.ByteArraySeekableStream”的类型不正确。它必须在org.mule.util.collection.EventToMessageSequenceSplittingStrategy上键入“{interface java.lang.Iterable,interface java.util.Iterator,interface org.mule.routing.MessageSequence,interface java.util.Collection}” \t .split(EventToMessageSequenceSplittingStrategy.java:65) – user4338724

+0

什么是你的输入类型dataweave?它是json还是其他的东西? – user02041988