2

来自 Json 响应的片段 -

[
    {
      "id": "ada2ac5d-57f1-4624-b35a-4fd34ada9e56",
      "userId": "userid",
      "firstName": "firstname",
      "lastName": "lastname",
      "dateAdded": "2018-07-06T04:19:21.57",
      "dateOfChange": "2018-07-06T04:19:22.14",
      "lastAction": "Updated",
   },
      {
      "id": "2f395120-5440-4770-8a2e-16a77aa69c1e",
      "userId": "userid",
      "firstName": "firstname",
      "lastName": "lastname",
      "dateAdded": "2018-07-06T04:19:21.57",
      "dateOfChange": "2018-07-06T04:19:21.99",
      "lastAction": "Updated",
   },
      {
      "id": "7aaa6b11-0f43-414b-a999-b03114392c21",
      "userId": "userid",
      "firstName": "firstname",
      "lastName": "lastname",
      "dateAdded": "2018-07-06T04:19:21.693",
      "dateOfChange": "2018-07-06T04:19:21.743",
      "lastAction": "Added",
   },
]

=======================

我想根据 id 匹配捕获“lastAction”值。

我试过以下 -

responseContent = testStepp.getPropertyValue("response") //getting response
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText(responseContent)

log.info "last action : : "

object.('$.[?(@.id=="ada2ac5d-57f1-4624-b35a-4fd34ada9e56")].lastAction')

结果 -

最后一个动作::[null,null,null,null,null,null,null,null,null,null]

4

2 回答 2

1

尝试使用 Groovy 的.find()功能:

object.find { it.id == 'ada2ac5d-57f1-4624-b35a-4fd34ada9e56' }.lastAction
于 2018-07-06T09:13:53.230 回答
1
object.find{it.id=="7aaa6b11-0f43-414b-a999-b03114392c21"}?.lastAction
于 2018-07-06T09:55:16.147 回答