You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WSO2 ESB中无法遍历JSON属性内的JSON数组问题

解决WSO2遍历静态JSON数组的问题

原配置无法遍历数组的核心问题是:

  • numbersList属性将完整JSON对象以字符串形式存储,foreach无法解析字符串为可遍历的数组
  • foreach的表达式没有指向JSON内部的数组节点

修正后的inSequence配置

<inSequence>
    <property name="numbersJSONOBJ" scope="default" type="STRING" value="{&quot;numbers&quot;:[1,2,3]}"/>
    <log level="custom">
        <property expression="get-property('numbersJSONOBJ')" name="numbers"/>
    </log>
    <!-- 直接遍历JSON中的numbers数组 -->
    <foreach expression="json-eval($ctx:numbersJSONOBJ.numbers)">
        <sequence>
            <log level="custom">
                <property name="MSG" value="INSIDE FOREACH"/>
            </log>
            <log level="custom">
                <property expression="json-eval($)" name="current_number"/>
            </log>
        </sequence>
    </foreach>
    <respond/>
</inSequence>

关键修正点

  1. 移除冗余的numbersList属性,直接在foreach的expression中通过json-eval($ctx:numbersJSONOBJ.numbers)定位到目标数组
  2. 调整日志输出为custom级别,更清晰打印单个数组元素

预期控制台日志

[2024-06-28 15:12:45,778]  INFO {LogMediator} - {api:NumbersAPI} numbers = {"numbers":[1,2,3]}
[2024-06-28 15:12:45,780]  INFO {LogMediator} - {api:NumbersAPI} MSG = INSIDE FOREACH
[2024-06-28 15:12:45,781]  INFO {LogMediator} - {api:NumbersAPI} current_number = 1
[2024-06-28 15:12:45,782]  INFO {LogMediator} - {api:NumbersAPI} MSG = INSIDE FOREACH
[2024-06-28 15:12:45,783]  INFO {LogMediator} - {api:NumbersAPI} current_number = 2
[2024-06-28 15:12:45,784]  INFO {LogMediator} - {api:NumbersAPI} MSG = INSIDE FOREACH
[2024-06-28 15:12:45,785]  INFO {LogMediator} - {api:NumbersAPI} current_number = 3

内容的提问来源于stack exchange,提问作者Youssef Gamal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 20:02:46