如何迭代JSON有效负载并在wso2中构造响应

时间:2017-02-17 06:17:25

标签: wso2esb

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

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"
   }]
}

1 个答案:

答案 0 :(得分:4)

您可以使用迭代,聚合和脚本调解器,正如我在此代理中所示。

<ul class="form-group mb-0">
    <label>Choose your holiday: </label>
    <li>
        <input type="radio" id="Tour" name="holiday" class="check" data-form="form1" checked>
        <label for="Tour">Tour</label>
    </li>
    <li>
        <input type="radio" id="Cruise" name="holiday" class="check" data-form="form2" />
        <label for="Cruise">Cruise</label>
   </li>
   <li>
        <input type="radio" id="Beach" name="holiday" class="check" data-form="form3" />
        <label for="Beach">Beach</label>
   </li>
</ul>

<form name="form1" id="form1" class="form active">
1
</form>
<form name="form2" id="form2"  class="form">
2
</form>
<form name="form3" id="form3"  class="form">
3
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(".check").click(function () {
    $(".form").removeClass("active");
     if ($(this).is(":checked")) {
        var form = $(this).data('form');
        //alert(form);
        $('#'+form).addClass("active");
     }
});
</script>

这是回复

<?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>

您还可以使用custom或payloadFactory介体生成Json响应

https://docs.wso2.com/display/ESB500/Writing+a+WSO2+ESB+Mediator