在json数组上迭代Mediator

时间:2014-08-01 10:23:36

标签: json loops wso2 esb

我想迭代json消息的结果数组

{ 
   "results":[ 
      { 
         "category":"mdl", 
         "subcategory":"ip", 
         "ip":"103.14.120.121", 
         "subtype":"IP-MDL", 
         "date":"1405074556", 
         "longitude":"75.3333", 
         "latitude":"19.8833", 
         "country":"India", 
         "city":"Aurangabad", 
         "organization":"Good Domain Registry Private Limited", 
         "isp":"Good Domain Registry Private Limited" 
      }, 
      { 
         "category":"mdl", 
         "subcategory":"ip", 
         "ip":"108.162.198.96", 
         "subtype":"IP-MDL", 
         "date":"1405074556", 
         "longitude":"-122.3933", 
         "latitude":"37.7697", 
         "country":"United States", 
         "city":"San Francisco", 
         "organization":"CloudFlare", 
         "isp":"CloudFlare" 
      } 
   ] 
} 

遗憾的是,迭代介体似乎不接受非xpath表达式 谁知道如何迭代上一个消息json结果与我在xml中的相同方式? 我想使用ESB json-native支持,但似乎不能像使用XML一样分解json消息(我已经尝试过迭代中介,json有效负载工厂和脚本调解器而没有任何成功)

            <iterate expression="json-eval(results)"> 
               <target> 
                  <sequence> 
                     <property name="messageType" value="application/json" scope="axis2"/> 
                     <payloadFactory media-type="json"> 
<format> 
{ 
"event":{ 
"providerName":"$1", 
"providerSource":"$2", 
"providerClassification":"$3", 
"providerVersion":"$4", 
"body":{ 
"results":{$5} 
} 
} 
} 
</format> 
<args> 
                                <arg value="bdigital"/> 
                                <arg evaluator="json" expression="$.results.category"/> 
<arg evaluator="json" expression="$.results.subcategory"/> 
                                <arg value="1.0"/> 
                                <arg evaluator="json" expression="$.results"/> 
</args> 
</payloadFactory> 
                     <call> 
                        <endpoint> 
                           <http method="post" 
                                 uri-template="http://BD-VM-PP-CIC01:8280/services/CICQueue"/> 
                        </endpoint> 
                     </call> 
                  </sequence> 
               </target> 
            </iterate>

提前感谢,

2 个答案:

答案 0 :(得分:3)

您可以执行以下操作吗?

<property name="messageType" value="application/xml" scope="axis2" type="STRING"></property>
      <iterate xmlns:ns="http://org.apache.synapse/xsd" continueParent="true" expression="//jsonObject/results" id="MyIterator">
         <target>
            <sequence>
               <property name="cat" expression="json-eval($.results.category)" type="STRING"></property>

答案 1 :(得分:0)

WSO2 EI中有一个新版本,您可以在其中使用JSON Path遍历JSON。您可以执行以下操作。

         <iterate id="iterate-over-users" preservePayload="true" attachPath="json-eval($.results)" expression="json-eval($.results)">
            <target>
               <sequence>
                  <send>
                     <endpoint>
                        <http method="POST" uri-template="http://backend.com"/>
                     </endpoint>
                  </send>
               </sequence>
            </target>
         </iterate>

有关更多信息,请参考以下博客。 https://medium.com/@arunans23/iterate-over-json-payload-with-wso2-enterprise-integrator-8ccb9cdd2c70