如何在 wso2 中迭代 JSON 有效负载并构建响应 [英] how to iterate JSON payload and construct the response in wso2

查看:24
本文介绍了如何在 wso2 中迭代 JSON 有效负载并构建响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过将carValue和bikeValue添加到Response数组的id("C_05"-"B_08"/"C_07"/"B_06")中来迭代结果数组并构造Response数组,并保持与描述相同的desc.

How to iterate through results array and construct Response array by adding carValue and bikeValue into id ("C_05"-"B_08"/"C_07"/"B_06") of the Response array and keep the same desc as description.

JSON 有效负载请求:

{"results": [
      {
      "desc": "Blind",
      "carValue": "05",
      "bikeValue": "08"
   },
      {
      "desc": "Deaf",
      "carValue": "09",
      "bikeValue": "10"
   },
{
      "desc": "Oxygen",
      "carValue": "07"
   },
{
      "desc": "carbon",
      "bikeValue": "06"
   }]
}

最终响应应该是:

{
  "Response" : [
   {
    "id" : "C_05"-"B_08",
    "description" : "Blind"
   },
   {
    "id" : "C_09"-"B_10",
    "description" : "Deaf"
   },
   {
    "id": "C_07",
    "description": "Oxygen"
   },
   {
    "id": "B_06",
    "description": "carbon"
   }]
}

推荐答案

您可以使用迭代、聚合和脚本中介,正如我在此代理中向您展示的那样.

You can use the iterate, aggregate and script mediators, as I show you in this proxy.

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="proxyIterateJson"
       startOnLoad="true"
       statistics="disable"
       trace="disable"
       transports="http,https">
   <target>
      <inSequence>
         <payloadFactory media-type="json">
            <format>
                                        {"results": [&#xD;
                                                  {&#xD;
                                                  "desc": "Blind",&#xD;
                                                  "carValue": "05",&#xD;
                                                  "bikeValue": "08"&#xD;
                                           },&#xD;
                                                  {&#xD;
                                                  "desc": "Deaf",&#xD;
                                                  "carValue": "09",&#xD;
                                                  "bikeValue": "10"&#xD;
                                           },&#xD;
                                        {&#xD;
                                                  "desc": "Oxygen",&#xD;
                                                  "carValue": "07"&#xD;
                                           },&#xD;
                                        {&#xD;
                                                  "desc": "carbon",&#xD;
                                                  "bikeValue": "06"&#xD;
                                           }]&#xD;
                                        }
                        </format>
            <args/>
         </payloadFactory>
         <iterate xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  attachPath="//jsonObject"
                  expression="//jsonObject/results"
                  id="it1"
                  preservePayload="true">
            <target>
               <sequence>
                  <property expression="$body//jsonObject/results/carValue"
                            name="carValue"
                            scope="default"
                            type="STRING"/>
                  <property expression="$body//jsonObject/results/bikeValue"
                            name="bikeValue"
                            scope="default"
                            type="STRING"/>
                  <property expression="$body//jsonObject/results/desc"
                            name="description"
                            scope="default"
                            type="STRING"/>
                  <script language="js">var carValue = mc.getProperty("carValue");
                                        var bikeValue = mc.getProperty("bikeValue");
                                        var desc = mc.getProperty("description");
                                        var concatValue;
                                        if(carValue == null || carValue == "") {
                                           concatValue = "B_" + bikeValue;
                                        } else if(bikeValue == null || bikeValue == "" ){
                                           concatValue = "C_" + carValue;
                                        }else {
                                           concatValue = "C_"+ carValue + "-" + "B_" + bikeValue;
                                        }
                                        print("Value concatenado: "+ concatValue );
                                        mc.setProperty("concatValue", concatValue);
                                        mc.setPayloadJSON({"result":{"id" : concatValue,"description" : desc}});</script>
                  <log>
                     <property expression="json-eval($.result.id)" name="JSON-Payload"/>
                  </log>
                  <loopback/>
               </sequence>
            </target>
         </iterate>
      </inSequence>
      <outSequence>
         <property name="res" scope="default">
            <ns:Response xmlns:ns="www.response.org"/>
         </property>
         <aggregate id="it1">
            <completeCondition>
               <messageCount max="-1" min="-1"/>
            </completeCondition>
            <onComplete enclosingElementProperty="res" expression="$body//result">
               <log level="custom" separator=",">
                  <property expression="json-eval($.)" name="OUPUTSECUENSE"/>
               </log>
               <send/>
            </onComplete>
         </aggregate>
      </outSequence>
      <faultSequence/>
   </target>
   <description/>
</proxy>

这是回复

{"Response":
    {"result":[
        {"id":"C_09-B_10","description":"Deaf"},
        {"id":"C_05-B_08","description":"Blind"},
        {"id":"C_07","description":"Oxygen"},
        {"id":"B_06","description":"carbon"}
]}}

您也可以使用 custom 或 payloadFactory 中介来生成 Json 响应

You can also use custom or payloadFactory mediator to generate the Json response

https://docs.wso2.com/display/ESB500/写作+a+WSO2+ESB+中介

这篇关于如何在 wso2 中迭代 JSON 有效负载并构建响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆