How to search/find In JSON with java

后端 未结 4 1794
忘了有多久
忘了有多久 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:24

    You can also use the JsonPath project provided by REST Assured. This JsonPath project uses Groovy GPath expressions. In Maven you can depend on it like this:

    
        com.jayway.restassured
        json-path
        2.4.0
    
    

    Examples:

    To get a list of all book categories:

    List categories = JsonPath.from(json).get("store.book.category");
    

    Get the first book category:

    String category = JsonPath.from(json).get("store.book[0].category");
    

    Get the last book category:

    String category = JsonPath.from(json).get("store.book[-1].category");
    

    Get all books with price between 5 and 15:

    List books = JsonPath.from(json).get("store.book.findAll { book -> book.price >= 5 && book.price <= 15 }");
    

    GPath is very powerful and you can make use of higher order functions and all Groovy data structures in your path expressions.

提交回复
热议问题