本文介绍了空手道:遍历复杂的JSON查找匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在达到一个API终点,并得到如下所示的内容.

I am hitting an API end point and getting something like below.

{
    "emp": {
        "id": "123",
        "firstNm": "test",
        "lastNm": "last",
        "dob": "200-01-01",
        "gender": {
            "code": "F",
            "name": "Female",
            "description": "Female"
        },
        "test1": [
            {
                "tes2": "F50045A3B994FB2BDF4E3D3FC906F592",
                "t2": "a23",
                "test3": {
                    "code": "432",
                },
                "ind": [
                    "ABC",
                    "BCD",
                ]
            }
        ]
    }
}

我要匹配数组中的元素

"ind": [
    "ABC",
    "BCD",
]

我尝试了以下方法:

 Feature: test
Background:
        * url BaseUrl
        * configure headers = read('classpath:headers.js')
Scenario Outline: test
    Given path '/path'
    And param id = <id>
    When method get
    Then status 200

        * def json = response
        * def has = {ind:['#string'] }
        * def indicator = { ind: '#[] has' }
        * match json.member == '#[] indicator'

Examples:
    |   id   |
    | '1234' |

但观察到以下异常:

预期:'#[] ind',原因:不是数组或列表有人可以帮忙吗?

expected: '#[] ind', reason: not an array or listCan someone please help?

推荐答案

我并没有真正遵循您的逻辑,因为指标不在json响应或定义的变量中,而是到达ind数组的路径是emp.test1 [0] .ind

I am not really following your logic since indicators is not in the json response or defined variable but to get to the ind array the path is emp.test1[0].ind

如果要匹配具有ABC和BCD的阵列,则应执行以下操作

if you want to match that the array has ABC and BCD you would do the following

* match response.emp.test1[0].ind == ['ABC', 'BCD']

这篇关于空手道:遍历复杂的JSON查找匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 18:34