【问题标题】:Transforming JSON array with WSO2使用 WSO2 转换 JSON 数组
【发布时间】:2017-11-09 16:28:59
【问题描述】:

我们是否知道如何将带有数组的 json 转换为带有其他字段的新 json。 我有示例 json。此示例 json 是动态的。数组中可以包含三个或四个元素。

"insurer": [ 
     {
    "data": {
        "first_name": "Name",
        "last_name": "SureName"
    }
    },
   {
    "data": {
        "first_name": "Name1",
        "last_name": "SureName1"
    }
}],

我想收到 json

    "insurer": [ 
     {
    "data": {
        "name": "Name",
        "nameLast": "SureName"
    }
    },
   {
    "data": {
        "name": "Name1",
        "nameLast": "SureName1"
    }
}],

我做了这个序列

 <foreach id="foreach_1" expression="//insurer/data">
        <sequence>
           <payloadFactory media-type="json">
              <format>{ "name" : "$1", 
                        "nameLast" : "$2"                            }                  
              </format>
              <args>
                 <arg evaluator="xml" expression="//first_name"/>
                 <arg evaluator="xml" expression="//last_name"/>
              </args>
           </payloadFactory>
           <log>
              <property name="message" value="petla"/>
           </log>
        </sequence>
     </foreach>

不幸的是,我只得到了部分 json。你可能有一个想法

 { ,"name" : "Name1", "nameLast" : "SureName1" }

我正在使用 WSO2 ESB V6.1.0

【问题讨论】:

    标签: json wso2 wso2esb wso2-am wso2carbon


    【解决方案1】:

    对您的序列进行 3 次更改后,这应该可以按预期工作。

    1. 首先将 foreach 调节器的表达式更改为 //insurer, 因为您的 JSON 消息有保险公司的数组。
    2. 接下来,使用 XML 作为 负载工厂调解器的媒体类型并组成单个 预期的保险人要素。
    3. 最后设置 messageType 属性将消息类型更改为 JSON。

    在下面找到示例代码。

         <foreach xmlns:ns="http://org.apache.synapse/xsd"
                  expression="//insurer">
            <sequence>
               <payloadFactory media-type="xml">
                  <format>
                    <insurer>
                       <data>
                          <name>$1</name>
                          <nameLast>$2</nameLast>
                       </data>
                    </insurer>
                  </format>
                  <args>
                     <arg evaluator="xml" expression="//first_name"/>
                     <arg evaluator="xml" expression="//last_name"/>
                  </args>
               </payloadFactory>
            </sequence>
         </foreach>
         <property name="messageType" value="application/json" scope="axis2"
                   type="STRING"/>
    

    【讨论】:

    • 非常感谢。这对我很有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 2015-05-24
    相关资源
    最近更新 更多