count members with jsonpath?

前端 未结 5 1104
生来不讨喜
生来不讨喜 2020-12-22 20:42

Is it possible to count the number of members using JsonPath?

Using spring mvc test I\'m testing a controller that generates

{\"foo\": \"oof\", \"ba         


        
5条回答
  •  误落风尘
    2020-12-22 21:13

    Been dealing with this myself today. It doesn't seem like this is implemented in the available assertions. However, there is a method to pass in an org.hamcrest.Matcher object. With that you can do something like the following:

    final int count = 4; // expected count
    
    jsonPath("$").value(new BaseMatcher() {
        @Override
        public boolean matches(Object obj) {
            return obj instanceof JSONObject && ((JSONObject) obj).size() == count;
        }
    
        @Override
        public void describeTo(Description description) {
            // nothing for now
        }
    })
    

提交回复
热议问题