Java 如何使用Rest Assured验证JSON 2D数组?

Java 如何使用Rest Assured验证JSON 2D数组?,java,json,rest,api,rest-assured,Java,Json,Rest,Api,Rest Assured,问题是 第1部分: { "people": [ { "name": "John", "address": "765 the the", "number": "3277772345", }, { "name": "Lee", "address": "456 no where", "number": "7189875432", }, ] } 我想验证数字字段,即数字是否为“7189

问题是

第1部分:

{
  "people": [
    {
      "name": "John",
      "address": "765 the the",
      "number": "3277772345",
    },
    {
      "name": "Lee",
      "address": "456 no where",
       "number": "7189875432",
    },  
  ]
}
我想验证数字字段,即数字是否为
“7189875432”
“7189875432”
的JSON路径是:
people[1]。编号(位于JSON数组中)

为此,我做了如下工作:

List<String> value=
given()
.when()
.get("/person")
.then()
.extract()
.path("people.findAll{it.number=='7189875432}. number");
 If (value.isEmpty)
            assert.fail(); 
现在我想验证电话号码
“987654321”
是否在JSON中。JSON路径:
people[1]。phoneno[2]。number

List<String> value=
given()
.when()
.get("/person")
.then()
.extract()
.path("people.phoneno.findAll{it.number=='987654321'}. number");
 If (value.isEmpty)
            assert.fail();
如果我这样做的话

 .path("people.phoneno. number");
I would get a list such as [["987654321", "3277772345", "7189875432", "8976542234"]] 
包含JSON中所有数字的列表

所以我的问题是,我们如何验证在另一个数组中包含一个数组的JSON路径?我不想硬编码任何东西


注意:唯一可用的信息是号码,即“987654321”

您可以随时编写自己的自定义匹配器:

private static Matcher<List<List<String>>> containsItem(String item) {
    return new TypeSafeDiagnosingMatcher<List<List<String>>>() {
      @Override
      protected boolean matchesSafely(List<List<String>> items, Description mismatchDescription) {
        return items.stream().flatMap(Collection::stream).collect(toList()).contains(item);
      }

      @Override
      public void describeTo(Description description) {
        description.appendText(
            String.format("(a two-dimensional collection containing \"%s\")", item));
      }
    };
  }
要使此匹配器更加可重用,请将输入类型设置为泛型:
私有静态匹配器containsItem(T项){…}

 .path("people.phoneno. number");
I would get a list such as [["987654321", "3277772345", "7189875432", "8976542234"]] 
private static Matcher<List<List<String>>> containsItem(String item) {
    return new TypeSafeDiagnosingMatcher<List<List<String>>>() {
      @Override
      protected boolean matchesSafely(List<List<String>> items, Description mismatchDescription) {
        return items.stream().flatMap(Collection::stream).collect(toList()).contains(item);
      }

      @Override
      public void describeTo(Description description) {
        description.appendText(
            String.format("(a two-dimensional collection containing \"%s\")", item));
      }
    };
  }
given()
.when()
.get("/person")
.then()
.body("people.phoneno.number", containsItem("987654321"));