放心 - 如何验证 JSON 同名响应字段

Rest assured - How to validate JSON response field with the same name

我有样本 JSON 回复

     "userinfo": [
            {
                "userName": "name1",
                "registered": false
            },
            {
                "userName": "name2",
                "registered": true
            }
          ]

我如何验证何时 userName = name1 然后 registered = falseuserName = name2 然后 registered = true

试过类似的方法,但没有用。

.body("$.findAll{it.userinfo.userName = name1"}.registered", equalTo(false));

我是不是漏掉了什么或做错了什么?

这里有一些东西可以帮助你,

    String json = "{\r\n" + "   \"userinfo\": [{\r\n" + "           \"userName\": \"name1\",\r\n"
            + "         \"registered\": false\r\n" + "      },\r\n" + "     {\r\n"
            + "         \"userName\": \"name2\",\r\n" + "           \"registered\": true\r\n" + "       }\r\n"
            + " ]\r\n" + "}";

    JsonPath js = new JsonPath(json);
    String emp1Name = js.get("userinfo.find {it.userName == 'name2'}.registered").toString();
    System.out.println(emp1Name);