2

我在 SOAP UI 中编写了一个测试用例,它创建一个用户并返回 Id。这是 JsonResponse。通过一个 Groovy 脚本,我需要提取id

{
    "schemas":["urn:hid:scim:api:ma:1.0:UserInvitation"],
    "urn:hid:scim:api:ma:1.0:UserInvitation":
        [
            {
                "meta":{ 
                    "resourceType":"UserInvitation",                                                   
                    "lastModified":"2015-12-22T07:45:30Z",
                    "location":"https://test-ma.api.assaabloy.com/credential-management/customer/663/invitation/2643209"
                },
                "invitationCode":"FBXO-SRWS-LKFI-ZKZI",
                "status":"PENDING",
                "createdDate":"2015-12-22T02:45:30Z",
                "expirationDate":"2015-12-22T02:45:30Z",
                "id":2643209
             }
         ]
}

我对 Groovy 很陌生。请帮我解决一下这个。

4

1 回答 1

3

这将是:

import groovy.json.JsonSlurper

def json = """
{
    "schemas":["urn:hid:scim:api:ma:1.0:UserInvitation"],
    "urn:hid:scim:api:ma:1.0:UserInvitation":
        [
            {
                "meta":{ 
                    "resourceType":"UserInvitation",                                                   
                    "lastModified":"2015-12-22T07:45:30Z",
                    "location":"https://test-ma.api.assaabloy.com/credential-management/customer/663/invitation/2643209"
                },
                "invitationCode":"FBXO-SRWS-LKFI-ZKZI",
                "status":"PENDING",
                "createdDate":"2015-12-22T02:45:30Z",
                "expirationDate":"2015-12-22T02:45:30Z",
                "id":2643209
             }
         ]
}"""
def slurped = new JsonSlurper().parseText(json)
assert 2643209 == slurped."urn:hid:scim:api:ma:1.0:UserInvitation"[0].id

new JsonSlurper().parseText(json)语句将返回地图的实例。因此,使用此映射,您可以通过以下语句获得列表:slurped."urn:hid:scim:api:ma:1.0:UserInvitation",然后使用getAt运算符 ( [0]) 获得第一个元素,最后id获得所需的值。

于 2015-12-22T08:24:36.653 回答