JSON 在其响应中获取总数记录的路径

JSON path for getting total number records in its response

我正在使用 wso2esb 4.9.0 和 Wso2dss3.0.0

我有一个来自 DSS 服务的 SOAP 响应,如下所示

 <soapenv:Header/>
   <soapenv:Body>
      <DataCollection xmlns="http://ws.wso2.org/dataservice">
         <Datalist>
            <partybranchid>2127629166052452</partybranchid>
            <partybranchname>other locations</partybranchname>
            <status>1</status>
            <partybranchtype>-1</partybranchtype>
            <partyid>460077453499443</partyid>
            <partyname>Yash Technologies Pvt Ltd</partyname>
            <ouid>-1</ouid>
            <organizationunitname>NONE</organizationunitname>
            <inchargeid>-1899999899</inchargeid>
            <sbuheadid>-1</sbuheadid>
            <usercriteriaconfigid>1187416899913503</usercriteriaconfigid>
            <primarymail>vikash@myunisoft.com</primarymail>
         </Datalist>
         <Datalist>
            <partybranchid>2545910581364239</partybranchid>
            <partybranchname>DBPB001</partybranchname>
            <status>1</status>
            <partybranchtype>2299733851146138</partybranchtype>
            <partyid>2534954639984412</partyid>
            <partyname>DBPRTY001</partyname>
            <ouid>2538210579514587</ouid>
            <organizationunitname>DBORG001</organizationunitname>
            <inchargeid>2540093122446574</inchargeid>
            <sbuheadid>1209449655666077</sbuheadid>
            <usercriteriaconfigid>1187416899913503</usercriteriaconfigid>
            <primarymail>vikash@myunisoft.com</primarymail>
         </Datalist>
      </DataCollection>
   </soapenv:Body>

从上面的 SOAP 响应中,我想将 Datalist 条记录的总数获取到我的服务中。为此,我给出了一个JSON 属性 文档,如下所示:

<property name="Total"
             expression="json-eval($.DataCollection.Datalist.length)"
             scope="default"
             type="STRING"/>

Total 应该得到 2 的值,因为它们是两条记录,如您所见;但这反而返回 null。我认为我提供的 JSON 路径不起作用。

计算记录数的正确JSON路径是什么?

实际上您似乎收到了来自 DSS 的 XML 文件。这就是 json 功能不适用于 xml 内容的原因。我设法使用脚本调解器

获取了元素的数量
<script language="js">var dl = mc.getPayloadXML()..*::Datalist;
   var cnt = dl.length();
   mc.setProperty("cnt", cnt);
</script>
<log level="full">
    <property name="cnt" expression="get-property('cnt')"/>
</log>

结果在正文上下文中得到 属性,名称为“cnt”,值为“2

假设您将 xml 消息转换为 json 并获得此 json 有效负载(完全有效)

{"DataCollection": [
    {"Datalist": {
            "partybranchid": 2127629166052452,
            "partybranchname": "other locations",
            "status": 1,
            "partybranchtype": -1,
            "partyid": 460077453499443,
            "partyname": "Yash Technologies Pvt Ltd",
            "ouid": -1,
            "organizationunitname": "NONE",
            "inchargeid": -1899999899,
            "sbuheadid": -1,
            "usercriteriaconfigid": 1187416899913503,
            "primarymail": "vikash@myunisoft.com"
        }
    },
    {"Datalist": {
            "partybranchid": 2545910581364239,
            "partybranchname": "DBPB001",
            "status": 1,
            "partybranchtype": 2299733851146138,
            "partyid": 2534954639984412,
            "partyname": "DBPRTY001",
            "ouid": 2538210579514587,
            "organizationunitname": "DBORG001",
            "inchargeid": 2540093122446574,
            "sbuheadid": 1209449655666077,
            "usercriteriaconfigid": 1187416899913503,
            "primarymail": "vikash@myunisoft.com"
        }
    }
]
}

然后JSONPATH表达式

$.DataCollection.length

returns 数组的值长度,在本例中等于“2”

有用的链接

或者以json格式提供你的负载。