当前位置: 首页 > 知识库问答 >
问题:

放心|如何使用 JSONpath 获取基于匹配 critera 的值列表

申屠恺
2023-03-14

我正在尝试使用JSON路径根据标准获取值列表,请放心。

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

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个答案

西门威
2023-03-14

以下表达对我有用。

response1.jsonPath().getList("collect { it.credentials.findAll { it.credentialType == 'Ban User Name'}.credentialId }.flatten()")
 类似资料:
  • 我需要使用REST保证Jsonpath根据匹配标准获取计数值 我尝试了以下方法,但不起作用: JSON:

  • 我试图使用Rest assured和JsonPath从pzSetRuleSetFilter字段中提取一个值,其中pyRuleSetName等于某个值(在本例中为商标)。我一直得到下面的错误。任何关于如何解决这个问题并提取价值的想法都是很好的 无效的JSON表达式:script1.groovy:1:需要EOF,发现“[”@第1行,第39列。 我的查询system.out.println(respon

  • 我花了很长时间弄清楚一些使用Jayways版本的JsonPath,我的查询如下: 但是,这不适用于Rest Assured,在执行时,我会看到以下错误: 是否需要以格式重写此表达式?如果是这样,那会是什么样子?或者,我可以以某种方式让Rest-Asken使用这个首选的JsonPath表达式吗? 下面是一段小的[WIP]代码,目前正在尝试使表达式工作,然后我将正确地修复该方法:

  • 我需要从从 Rest Assured 建模请求返回的 JSON 响应数据中获取两个值: 这抛出和 获取这些的正确、最好的方法是什么?我知道我可以链接关闭请求,但不确定如何获取

  • 我有一个类和一个注释 我想写一个 aspectj 切入点,它将捕获所有具有 value=true 的注释方法注释的方法。我该怎么做?

  • 今天下午,我一直在努力寻找一种方法,通过检查熊猫名字(标签?)中某个模式的出现,来选择我的熊猫数据目录中的几列。 (当然,可以应用任何函数) 然后,我找到了函数,并让它使用以下代码: 当然,在第一个解决方案中,我可以执行相同类型的regex检查,因为我可以将它应用于迭代返回的数据类型。