【问题标题】:mule expression for checking json file用于检查 json 文件的 mule 表达式
【发布时间】:2016-03-31 15:15:02
【问题描述】:

是否可以编写一个 mule 表达式来检查以下 json 请求中 Name 的所有值是否相同?

我正在尝试类似#[($ in message.payload.id.items if $.Name==$.Name)] 但它不起作用 请建议

{"id":
    {
     "items" : [
    {
    "Name": "Raj",
    "transaction_tag": "value1",
    "transaction_type": "withdraw"
    },
{
    "Name": "Raj",
    "transaction_tag": "value2",
    "transaction_type": "submit"
    },
  {
    "Name": "Raj",
    "transaction_tag": "value3",
    "transaction_type": "inquiry"
    }
]
}
}

【问题讨论】:

    标签: java mule mule-el


    【解决方案1】:

    我认为更简洁的实现是使用 JSON 到对象转换器将 JSON 映射到 POJO(Java 对象)。

    例如:

    <json:json-to-object-transformer returnClass="com.mycompany.Request" doc:name="JSON to Request"
                doc:description="Convert the JSON payload to a Java object for further processing." />
    

    您可以将您的 POJO 命名为“请求”,如上例所示,并作为项目列表的成员,以及名称为 hasRepeatedItems() 的布尔方法,如果项目重复或不重复,则返回 true 或 false。

    在您的 JSON 通过您的转换器(成功)之后,您的有效负载现在将是一个 Java 对象“请求”。

    您可以使用选择路由器并调用方法 payload.hasRepeatedItems()。

    <choice doc:name="Choice">
                <when expression="payload.hasRepeatedItems()">
                    Do something ...
                </when>
                <otherwise>
                    Do something else...
                </otherwise>
    </choice>
    

    我希望这会有所帮助。如果我需要进一步说明,请让我结。

    【讨论】:

      【解决方案2】:

      您可以使用以下代码检查所有Name元素是否具有相同的值='Raj':-

      <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
      
       <flow name="testFlow">
          <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
          <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
              <foreach collection="#[message.payload.id.items]" doc:name="For Each">
                      <choice doc:name="Choice">
                          <when expression="#[message.payload.Name=='Raj']">
                              <logger level="INFO" doc:name="Logger" message="Equal values"/>
                          </when>
                       <otherwise>
                              <logger message="Not equal" level="INFO" doc:name="Logger"/>
                          </otherwise>
                      </choice>
               </foreach>
               <json:object-to-json-transformer doc:name="Object to JSON"/>
          </flow>
      

      替代选项:-
      如果您知道 JSON 列表中元素的编号将是固定的(例如,在这种情况下为 3 Name 元素),您可以尝试以下操作:-

       <choice doc:name="Choice">
          <when expression="#[message.payload.id.items[0].Name==message.payload.id.items[1].Name and message.payload.id.items[1].Name==message.payload.id.items[2].Name and message.payload.id.items[2].Name==message.payload.id.items[0].Name]">
              <logger level="INFO" doc:name="Logger" message="Equal"/>
            </when>
            <otherwise>
               <logger message="Not equal" level="INFO" doc:name="Logger"/>
             </otherwise>
        </choice>
      

      但最好的选择是遵循上述解决方案,这对任何数量的元素都有好处

      【讨论】:

        【解决方案3】:

        @Anirban.. 对于每个循环解决方案都很棒.. 但是它的硬编码名称您必须再次检查整个数组的所有名称并从中计数。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-04
          • 1970-01-01
          • 1970-01-01
          • 2017-07-29
          相关资源
          最近更新 更多