How to search/find In JSON with java

后端 未结 4 1793
忘了有多久
忘了有多久 2020-12-05 20:30

I have a below JSON string from the below i want to find/search criteria in JSON String.

1). To find the Number of keys present. 2). To get the values of given key(

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 21:04

    Firstly, add json-path dependency in pom

    
         com.jayway.jsonpath
         json-path
         2.2.0
    
    

    Create a utility in Base Class that can be reused :

     public static String getValueFromJsonPath(String apiResponse, String jsonPath) {
            return JsonPath.read(apiResponse, jsonPath);
        }
    

    One example of using this method :

    getValueFromJsonPath(apiResponse, "$.store.book[0].category");
    

    NOTE : Create overloaded method with different return types depending upon the expected result (like List for fetching a list from json)

提交回复
热议问题