zoukankan      html  css  js  c++  java
  • json-path解析json方便可靠

    JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPath is available in many programming languages such as Javascript, Python and PHP. Now also in Java!
    News
     
    2013-09-27 Released 0.9.0 bug fixes, general improvements
     
    2012-04-16 Released 0.8.1 bug fixes, improved docs, general improvements
     
    2012-03-08 Released 0.8.0 bug fixes, Filter builder, Json model, POJO mapping (optional) and compliance improvements.
     
    2012-02-09 Released 0.5.6 including bug fixes and performance improvements.
    Given
     
    { "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
        }
      }
    }
     
    Read
     
    All authors:
     
    List<String> authors = JsonPath.read(json, "$.store.book[*].author");
     
    Author of first book in store:
     
    String author = JsonPath.read(json, "$.store.book[1].author");
     
    All books with category = "reference"
     
    List<Object> books = JsonPath.read(json, "$.store.book[?(@.category == 'reference')]");
     
    List<Object> books = JsonPath.read(json, "$.store.book[?]", filter(where("category").is("reference")));
     
    All books that cost more than 10 USD
     
    List<Object> books = JsonPath.read(json, "$.store.book[?(@.price > 10)]");
     
    List<Object> books = JsonPath.read(json, "$.store.book[?]", filter(where("price").gt(10)));
     
    All books that have isbn
     
    List<Object> books = JsonPath.read(json, "$.store.book[?(@.isbn)]");
     
    List<Object> books = JsonPath.read(json, "$.store.book[?]", filter(where("isbn").exists(true)));
     
    Chained filters
     
    Filter filter = Filter.filter(Criteria.where("isbn").exists(true).and("category").in("fiction", "reference"))
     
    List<Object> books = JsonPath.read(json, "$.store.book[?]", filter);
     
    Custom filters
     
    Filter myFilter = new Filter.FilterAdapter<Map<String, Object>>(){
                    @Override
                    public boolean accept(Map<String, Object> map) {
                         return map.containsKey("isbn");  
                    }
                };
     
    List<Object> books = JsonPath.read(json, "$.store.book[?]", myFilter);
     
    All prices in the document
     
    List<Double> prices = JsonPath.read(json, "$..price");
     
    Compiled path
     
    You can pre compile a path and use it multiple times
     
    JsonPath path = JsonPath.compile("$.store.book[*]");
     
    List<Object> books = path.read(json);
     
    Assert
     
    Asserts are made with Hamcrest matchers
     
    JsonAssert.with(json).assertThat("$.store.bicycle.color", Matchers.equalTo("red"))
              .assertThat("$.store.bicycle.price", Matchers.equalTo(19.95D));
     
    Add some static imports and you get this
     
    with(json).assertThat("$.store.bicycle.color", equalTo("red"))
              .assertThat("$.store.bicycle.price", equalTo(19.95D));
     
    The Hamcrest library contains a lot of different matchers and they can often be nested.
     
    with(json).assertThat("$..author", hasItems("Nigel Rees", "Evelyn Waugh"))
              .assertThat("$..author", is(collectionWithSize(equalTo(2))));
     
    with(json).assertThat("$.store.book[?(@.category == 'x')]", emptyCollection());
     
    If you don't find the matcher you need, roll your own.
    Download
     
    Json-path is available at Maven Central
     
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <version>0.9.1</version>
    </dependency>
     
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path-assert</artifactId>
        <version>0.9.1</version>
        <scope>test</scope>
    </dependency>
  • 相关阅读:
    可视化理解卷积神经网络
    方差 标准差区别
    SSIS中出现数据流数据源假死状态的解决办法
    IIS中ASP.NET虚拟目录不继承主站点web.config设置的办法(转载)
    SSAS 度量值中的distinct count局聚合方式会数为null的值
    SSAS中CUBE的多对多关系既可以出现在中间事实表上也可以出现在中间维度表上
    SSAS中事实表中的数据如果因为一对多或多对多关系复制了多份,在维度上聚合的时候还是只算一份
    获取当前正在执行的Javascript脚本文件的路径
    C#中try catch中throw ex和throw方式抛出异常有何不同
    SqlServer 一个查询语句导致tempdb增大55G(转载)
  • 原文地址:https://www.cnblogs.com/ceshi2016/p/6813887.html
Copyright © 2011-2022 走看看