REST保证|如何使用JSONpath基于匹配的critera获取值列表

时间:2015-05-25 13:00:53

标签: json rest rest-assured rest-assured-jsonpath

我正在尝试使用JSON路径REST保证基于条件获取值列表。

以下是我的JSON响应,我想获取credentialId的值,其中credentialType“是”禁止用户名“,我尝试了下面的代码,但是它为我返回了空值。可以请一些正确的JSON路径帮助我获取所需的值。

List<Object> object1 = response1.jsonPath().getList("credentials.findAll { it.credentialType=='Ban User Name' }");


[
    {
        "addresses": [

        ],
        "dateOfBirth": null,
        "credentials": [
            {
                "credentialId": "mregasapgreene",
                "credentialType": "Ban User Name",
                "effectiveEndDate": null,
                "effectiveStartDate": null
            },
            {
                "credentialId": "MGREENE",
                "credentialType": "Ban ID",
                "effectiveEndDate": null,
                "effectiveStartDate": null
            }
        ],
        "dateDeceased": null,
        "emails": [

        ],
        "ethnicity": null,
        "guid": "3b020aed-1c69-469c-889d-301e8abce78c",
        "maritalStatus": null,
        "metadata": {
            "dataOrigin": "GRAILS"
        },
        "names": [
            {
                "firstName": "Mallory",
                "lastName": "(Reg as AP) Greene",
                "middleName": "A",
                "nameType": "Primary",
                "pedigree": null,
                "preferredName": null,
                "lastNamePrefix": null,
                "title": null
            }
        ],
        "phones": [

        ],
        "races": [

        ],
        "roles": [
            {
                "effectiveEndDate": null,
                "effectiveStartDate": null,
                "role": "Student"
            }
        ],
        "gender": null
    },
    {
        "addresses": [
            {
                "addressType": "Mailing",
                "city": "Seattle",
                "country": null,
                "county": null,
                "region": "WA",
                "streetAddress1": "123 Sesame Street",
                "streetAddress2": null,
                "streetAddress3": null,
                "postalCode": "98199"
            },
            {
                "addressType": "Home",
                "city": "Malvern",
                "country": {
                    "value": "United States of America",
                    "code": "US"
                },
                "county": null,
                "region": "PA",
                "streetAddress1": "412 Two Street",
                "streetAddress2": null,
                "streetAddress3": null,
                "postalCode": "19355"
            }
        ],
        "dateOfBirth": null,
        "credentials": [
            {
                "credentialId": "937",
                "credentialType": "Ban Sourced ID",
                "effectiveEndDate": null,
                "effectiveStartDate": null
            },
            {
                "credentialId": "s11",
                "credentialType": "Ban User Name",
                "effectiveEndDate": null,
                "effectiveStartDate": null
            },
            {
                "credentialId": "025EB9EF4420F0D0E05018958B283255",
                "credentialType": "Ban UDC ID",
                "effectiveEndDate": null,
                "effectiveStartDate": null
            },
            {
                "credentialId": "A00000718",
                "credentialType": "Ban ID",
                "effectiveEndDate": null,
                "effectiveStartDate": null
            }
        ],
        "dateDeceased": null,
        "emails": [

        ],
        "ethnicity": null,
        "guid": "3f86c5e3-08b8-431b-bdc5-3ee250847976",
        "maritalStatus": null,
        "metadata": {
            "dataOrigin": null
        },
        "names": [
            {
                "firstName": "Student",
                "lastName": "101",
                "middleName": null,
                "nameType": "Primary",
                "pedigree": null,
                "preferredName": null,
                "lastNamePrefix": null,
                "title": null
            }
        ],
        "phones": [
            {
                "phoneExtension": null,
                "phoneNumber": "+1 12",
                "phoneType": "Home"
            }
        ],
        "races": [

        ],
        "roles": [
            {
                "effectiveEndDate": null,
                "effectiveStartDate": null,
                "role": "Student"
            }
        ],
        "gender": null
    },
    {
        "addresses": [
            {
                "addressType": "Mailing",
                "city": "Seattle",
                "country": null,
                "county": null,
                "region": "WA",
                "streetAddress1": "123 Sesame Street",
                "streetAddress2": null,
                "streetAddress3": null,
                "postalCode": "98199"
            }
        ],
        "dateOfBirth": null,
        "credentials": [
            {
                "credentialId": "938",
                "credentialType": "Ban Sourced ID",
                "effectiveEndDate": null,
                "effectiveStartDate": null
            },
            {
                "credentialId": "s12",
                "credentialType": "Ban User Name",
                "effectiveEndDate": null,
                "effectiveStartDate": null
            },
            {
                "credentialId": "025EB9EF5535F0D0E05018958B283255",
                "credentialType": "Ban UDC ID",
                "effectiveEndDate": null,
                "effectiveStartDate": null
            },
            {
                "credentialId": "A00000719",
                "credentialType": "Ban ID",
                "effectiveEndDate": null,
                "effectiveStartDate": null
            }
        ],
        "dateDeceased": null,
        "emails": [
            {
                "emailAddress": "ilpstudent102@test.eln.com",
                "emailType": "Personal",
                "guid": "2a7d3ba4-9ec6-49b3-a3f8-a9e757f756a4"
            },
            {
                "emailAddress": "ilpstudent102@test.eln.com",
                "emailType": "Preferred",
                "guid": "2a7d3ba4-9ec6-49b3-a3f8-a9e757f756a4"
            }
        ],
        "ethnicity": null,
        "guid": "06c93bce-a380-4ff4-83a4-3a92e7cb18db",
        "maritalStatus": null,
        "metadata": {
            "dataOrigin": null
        },
        "names": [
            {
                "firstName": "Student",
                "lastName": "102",
                "middleName": null,
                "nameType": "Primary",
                "pedigree": null,
                "preferredName": null,
                "lastNamePrefix": null,
                "title": null
            }
        ],
        "phones": [

        ],
        "races": [

        ],
        "roles": [
            {
                "effectiveEndDate": null,
                "effectiveStartDate": null,
                "role": "Student"
            }
        ],
        "gender": null
    }
]

1 个答案:

答案 0 :(得分:1)

以下表达对我有用。

response1.jsonPath().getList("collect { it.credentials.findAll { it.credentialType == 'Ban User Name'}.credentialId }.flatten()")