您好,我想问如何检索 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"));
输出
response.prettyPrint() = 5a99dcf84b7f633a5489805d,5a99dcf84b7f633a5489805d
现在它正在打印所有 transactionId。它应该只拉出最新的一个,即最下面的一个,其中 transactionId = "5a99dd3f4b7f633a54898068"
如果有新值进来(通过后端逻辑,将向其中添加另一组值)。如何编写一行来提取最新的值集?
示例
{
"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]的内容
最佳答案
好吧,您可以使用 getList
而不是使用 getString
例如
List<String> strList = response.jsonPath().getList("testing.transactionId");
System.out.println("last value is " + strList.get(strList.size()-1))
关于java - 如何在Java中提取最后一个jsonPath值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49079571/