jsonPath 的使用记录

  |   0 评论   |   359 浏览

    784a8320220844c3bdb0a5bd05abb547-image.png

    {
      "store": {
        "book": [ 
          { "category": "reference",
            "author": "Nigel Rees",
            "title": "Sayings of the Century",
            "price": 8.95
          },
          { "category": "fiction",
            "author": "Evelyn Waugh",
            "title": "Sword of Honour",
            "price": 12.99,
            "isbn": "0-553-21311-3"
          }
        ],
        "bicycle": {
          "color": "red",
          "price": 19.95
        }
      }
    }
    
    private static void jsonPathTest() {
    
        JSONObject json = jsonTest();//调用自定义的jsonTest()方法获得json对象,生成上面的json
    
        //输出book[0]的author值
        String author = JsonPath.read(json, "$.store.book[0].author");
    
        //输出全部author的值,使用Iterator迭代
        List authors = JsonPath.read(json, "$.store.book[*].author");
    
        //输出book[*]中category == 'reference'的book
        List books = JsonPath.read(json, "$.store.book[?(@.category == 'reference')]");              
    
        //输出book[*]中price>10的book
        List books = JsonPath.read(json, "$.store.book[?(@.price>10)]");
    
        //输出book[*]中含有isbn元素的book
        List books = JsonPath.read(json, "$.store.book[?(@.isbn)]");
    
        //输出该json中所有price的值
        List prices = JsonPath.read(json, "$..price");
    
        //可以提前编辑一个路径,并多次使用它
        JsonPath path = JsonPath.compile("$.store.book[*]");
    
        List books = path.read(json);
    
    }
    

    评论

    发表评论

    validate