代码之家  ›  专栏  ›  技术社区  ›  user3483129

检查mule中的json键或对象属性

  •  1
  • user3483129  · 技术社区  · 10 年前

    场景-我必须遍历这个负载,对于那些有错误的列表,我需要增加计数。但如何检查错误属性是否存在?

    {
        "jobGuid": "123",
        "status": "COMPLETED",
        "listings": [
            {
                "exteralListingId": 7654320
            },
            {
                "exteralListingId": 7654321,
                "error": {
                    "code": "inventory.listings.sellerCreditCardNotfound",
                    "description": "Seller credit card not found"
                }
            }
        ]
    }
    

    选项1-使用json语法检查
    选项2-在列表上为每个循环循环,检查#[payload.error!=null]。但它给出了错误-消息负载的类型为:LinkedHashMap

    1 回复  |  直到 10 年前
        1
  •  1
  •   Eddú Meléndez    10 年前

    您可以使用类似xpath的jsonPath,但对于JSON

    我用提供的json附上了我的示例。正如你所看到的 #[json:listings] 该数组将由 foreach 然后使用验证是否包含错误标记 #[json:error] . errorCount 变量存储错误数,并将在控制台中打印。

    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
        <flow name="demoFlow">
            <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
            <set-payload value="{&quot;jobGuid&quot;:&quot;123&quot;,&quot;status&quot;:&quot;COMPLETED&quot;,&quot;listings&quot;:[{&quot;exteralListingId&quot;:7654320},{&quot;exteralListingId&quot;:7654321,&quot;error&quot;:{&quot;code&quot;:&quot;inventory.listings.sellerCreditCardNotfound&quot;,&quot;description&quot;:&quot;Seller credit card not found&quot;}},{&quot;exteralListingId&quot;:7654321,&quot;error&quot;:{&quot;code&quot;:&quot;inventory.listings.sellerCreditCardNotfound&quot;,&quot;description&quot;:&quot;Seller credit card not found&quot;}},{&quot;exteralListingId&quot;:7654321,&quot;error&quot;:{&quot;code&quot;:&quot;inventory.listings.sellerCreditCardNotfound&quot;,&quot;description&quot;:&quot;Seller credit card not found&quot;}}]}" doc:name="Set Payload"/>     
            <expression-transformer expression="#[json:listings]" doc:name="Expression"/>
            <set-variable variableName="errorCount" value="#[0]" doc:name="Variable"/>
            <foreach collection="#[message.payload]" doc:name="For Each">
                <expression-filter expression="#[json:error]" doc:name="Expression"/>
                <set-variable variableName="errorCount" value="#[flowVars.errorCount + 1 ]" doc:name="Variable"/>
                <logger message="counter: #[errorCount]" level="INFO" doc:name="Logger"/>
            </foreach>
        </flow>
    

    有关更多信息,请查看mule的官方文档。 http://www.mulesoft.org/documentation/display/current/JSON+Module+Reference