0

我需要从我的 json 响应中提取最后一个元素,我正在使用 json 后处理器。

json 路径 -

$.self[(@.length-1)].cashCardId

使用此路径后,我收到此错误 -

例外:无法解析从位置 6 开始的标记。应为 ?、'、0-9、*

经历错误后,我尝试了这个 jsonpath,但它抛出“NO MATCH”

$.self['(@.length-1)'].cashCardId

但是当我在在线评估器中评估我的 jsonPath 时,它工作正常。看起来 jmeter 源代码中存在任何类型的错误,或者可能是我产生了一些语法错误。

{
    "gift": [
    ],
    "self": [
         {
            "cashInitialAmount": 2000.0,
            "serialNumber": "M79H3LSU",
            "creationTime": 1563783465000,
            "endDate": "2036-01-01",
            "cardType": "ECARD",
            "buyerId": 397,
            "cashPendingAmount": 2000.0,
            "cardSerial": "M79H3LSU",
            "purchaseOrderId": 6123682,
            "initialOffer": 0.0,
            "customerId": 397,
            "offerId": null,
            "cashCardId": 188503,
            "purchaseTime": 1563783465000,
            "lastModified": 1563783465000,
            "activationTime": 1563783465000,
            "cardNumber": "M79H3LSU",
            "cardStatus": "ACTIVE",
            "startDate": "2019-07-22"
        },
        {
            "cashInitialAmount": 2000.0,
            "serialNumber": "2OI5U2UV",
            "creationTime": 1563783382000,
            "endDate": "2036-01-01",
            "cardType": "ECARD",
            "buyerId": 397,
            "cashPendingAmount": 2000.0,
            "cardSerial": "2OI5U2UV",
            "purchaseOrderId": 6123680,
            "initialOffer": 0.0,
            "customerId": 397,
            "offerId": null,
            "cashCardId": 188502,
            "purchaseTime": 1563783382000,
            "lastModified": 1563783382000,
            "activationTime": 1563783382000,
            "cardNumber": "2OI5U2UV",
            "cardStatus": "ACTIVE",
            "startDate": "2019-07-22"
        },
        {
            "cashInitialAmount": 2000.0,
            "serialNumber": "S7EJDE77",
            "creationTime": 1563794985000,
            "endDate": "2036-01-01",
            "cardType": "ECARD",
            "buyerId": 397,
            "cashPendingAmount": 2000.0,
            "cardSerial": "S7EJDE77",
            "purchaseOrderId": 6123723,
            "initialOffer": 0.0,
            "customerId": 397,
            "offerId": null,
            "cashCardId": 188511,
            "purchaseTime": 1563794985000,
            "lastModified": 1563794985000,
            "activationTime": 1563794985000,
            "cardNumber": "S7EJDE77",
            "cardStatus": "ACTIVE",
            "startDate": "2019-07-22"
        }
    ]
}
4

1 回答 1

0

无论出于何种原因,Goessner 的 jsonpath(JavaScript,https: //goessner.net/articles/JsonPath/)与 Jayway 实现(Java,https: //github.com/json-path/JsonPath)的 jsonpath 语法略有不同)

一方面,length是 Goessner 中的一个数组属性,但在 Jayway 中它是一个函数,因此,例如,$.self.length对于 Goessner 但不适用于 Jayway,你需要$.self.length()

对于Jayway'sarray[-1]会给你最后的元素。所以对于你的情况,表达式$.self[-1].cashCardId应该给你你想要的。

可以在这里测试不同的实现:https ://jsonpath.herokuapp.com/

于 2019-07-24T17:41:28.530 回答