如何在Java中提取最后一个jsonPath值?

如何在Java中提取最后一个jsonPath值?,java,jsonpath,rest-assured-jsonpath,Java,Jsonpath,Rest Assured Jsonpath,您好,我想问一下如何检索jsonPath中的最后一个值 { "testing": [ { "transactionId": "5a99dcf84b7f633a5489805d", "trackingId": "555112", "tn": "6095555112", "customerName": "John", "customerLastName":

您好,我想问一下如何检索jsonPath中的最后一个值

{
    "testing": [
        {
            "transactionId": "5a99dcf84b7f633a5489805d",
            "trackingId": "555112",
            "tn": "6095555112",
            "customerName": "John",
            "customerLastName": "Chan"
        },
        {
            "transactionId": "5a99dd3f4b7f633a54898068",
            "tn": "6095555112",
            "trackingId": "555112",
            "customerName": "Amanda",
            "customerLastName": "Brown"

        }
    ]
}
我的Java代码示例行

System.out.println("response.prettyPrint() = " + response.jsonPath().getString("testing.transactionId"));
{
    "testing": [
        {
            "transactionId": "5a99dcf84b7f633a5489805d",
            "trackingId": "555112",
            "tn": "6095555112",
            "customerName": "John",
            "customerLastName": "Chan"
        },
        {
            "transactionId": "5a99dd3f4b7f633a54898068",
            "tn": "6095555112",
            "trackingId": "555112",
            "customerName": "Amanda",
            "customerLastName": "Brown"

        },
        {
            "transactionId": "newID",
            "tn": "6095555112",
            "trackingId": "555112",
            "customerName": "Amanda",
            "customerLastName": "Brown"

        },
    ]
}
输出

response.prettyPrint() = 5a99dcf84b7f633a5489805d,5a99dcf84b7f633a5489805d
现在它正在打印所有TransactionID。它应该只拉取最新的一个,即transactionId=“5a99dd3f4b7f633a54898068”处的底部一个

如果一个新值进入(通过后端逻辑,将向其中添加另一组值)。如何编写一行来提取最新的值集

示例

System.out.println("response.prettyPrint() = " + response.jsonPath().getString("testing.transactionId"));
{
    "testing": [
        {
            "transactionId": "5a99dcf84b7f633a5489805d",
            "trackingId": "555112",
            "tn": "6095555112",
            "customerName": "John",
            "customerLastName": "Chan"
        },
        {
            "transactionId": "5a99dd3f4b7f633a54898068",
            "tn": "6095555112",
            "trackingId": "555112",
            "customerName": "Amanda",
            "customerLastName": "Brown"

        },
        {
            "transactionId": "newID",
            "tn": "6095555112",
            "trackingId": "555112",
            "customerName": "Amanda",
            "customerLastName": "Brown"

        },
    ]
}
既然存储了一个新的数据集,我将如何编写一个java代码来提取transactionId“NewID”

我不想硬编码和编写类似“.transactionId[0]”[1]或[2]

使用Gson api的东西

它转换json-->java对象


我希望这会有所帮助。我不知道确切的代码,但我知道在数组中,你可以得到最后一项,如

 ARRAY.get(ARRAY.size()-1) 

我不知道如何在JSON数组中正确地实现这一点,但我希望这会给您带来正确的方向

不用
getString
你可以使用
getList
例如

List strList=response.jsonPath().getList(“testing.transactionId”);
System.out.println(“最后一个值是”+strList.get(strList.size()-1))

工作起来很有魅力!非常感谢你,阿萨尔。