programing

jsonpath 식을 사용한 어레이 크기 - Stefan Goessner JsonPath

yellowcard 2023. 3. 6. 20:58
반응형

jsonpath 식을 사용한 어레이 크기 - Stefan Goessner JsonPath

Stefan Goessner의 Json Path를 사용하여 어레이 또는 목록 크기를 찾는 데 문제가 있습니다.버전 json-path-2.0.0을 사용하고 있습니다.

제 jsonpath 표현은$.orders.lengthJSON은 다음과 같습니다.

{
  "orders" : [
    ...
  ]
}

다음 오류로 인해 실패합니다.

com.jayway.jsonpath.PathNotFoundException: Property ['length'] not found in path $['orders']

그리고 저는$.orders.length()다음 에러로 인해 다시 장애가 발생하고 있습니다.

com.jayway.jsonpath.PathNotFoundException: Property ['length()'] not found in path $['orders']

Goessner의 JsonPath 식을 사용하여 어레이 길이를 구하는 방법을 알려주세요.

[EDIT] 설정을 입수하는 방법은 다음과 같습니다.

    com.jayway.jsonpath.Configuration conf = com.jayway.jsonpath.Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
    DocumentContext documentContext = JsonPath.using(conf).parse(orderJson);
    Object val = documentContext.read(jsonPathExpression);

반품 지원이 필요한 것 같습니다.length()jayway json-path 라이브러리의 버전 2.1.0에만 어레이가 추가되었습니다.

몇 가지 간단한 테스트를 통해$.orders.length()expression은 버전 2.1.0과 버전 2.2.0 모두에서 동작하는 것 같기 때문에 표시되는 오류를 수정하려면 종속 버전을 업그레이드해야 한다고 생각합니다.

spring webflux에서 webclient 사용

....
 .jsonPath("$.length()").isEqualTo(2);
...

json 배열의 요소 수를 계산하는 방법은 다음과 같습니다.

myJson->내부에는 어레이가 존재한다Json

fieldPath-> 어레이 경로

import com.jayway.jsonpath.JsonPath;
import org.json.JSONArray;
import org.json.simple.JSONObject;

JSONObject myJson;

    public long CountFields(String fieldPath) {
      String onlyMyArray=JsonPath.parse(myJson).read(fieldPath).toString();
      JSONArray jsonArray = new JSONArray(onlyMyArray);
      return jsonArray.length();
                       }
            JsonPath js = new JsonPath(response);
           List values = js.getList("Mention the path of the json here");
           int size= values.size();
           System.out.println(size);
List values = jsonpath.getList("orders");
System.out.println("Size of object : "+ values.size());

언급URL : https://stackoverflow.com/questions/36964936/array-size-using-jsonpath-expression-stefan-goessner-jsonpath

반응형