JsonPath解析Json数据

JsonPath to JSON is 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!

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 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 books = JsonPath.read(json, "$.store.book[?(@.category == 'reference')]");


List books = JsonPath.read(json, "$.store.book[?]", filter(where("category").is("reference")));


All books that cost more than 10 USD


List books = JsonPath.read(json, "$.store.book[?(@.price > 10)]");


List books = JsonPath.read(json, "$.store.book[?]", filter(where("price").gt(10)));


All books that have isbn


List books = JsonPath.read(json, "$.store.book[?(@.isbn)]");


List 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 books = JsonPath.read(json, "$.store.book[?]", filter);


Custom filters


Filter myFilter = new Filter.FilterAdapter>(){

                @Override

                public boolean accept(Map map) {

                     return map.containsKey("isbn");  

                }

            };


List books = JsonPath.read(json, "$.store.book[?]", myFilter);


All prices in the document


List 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 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,add below dependency into pom.xml :


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,940评论 0 23
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • json: [{"text": "\u201cThe world as we have created it is...
    Sol_elY阅读 625评论 1 0
  • 队列: 先进先出 栈: 先进后出 1.使用Queue实现生产者与消费者解耦 可以使用队列来实现线程间的同步 生产者...
    小灰辉先生阅读 127评论 0 1
  • 我的一个朋友映岚和泽洋,他们在一起三年了 ,可最后还是分开了 。 有人说爱情是败给距离 ,有人说爱情是败给时间,还...
    薄雾花香雪凝白阅读 290评论 0 1