Using JSONPath in your Java application
There's an implementation of JSONPath for Java, too, written by Jayway. It's available from GitHub, or you can obtain it through the Central Maven Repository if your project uses the Maven build system. It matches the original JSONPath API, returning Java objects and collections for fields in JSON objects.
Getting ready
You'll need to either download the code from GitHub at https://github.com/jayway/JsonPath, or, if you're using Maven as your build system, include the following dependency:
<dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <version>2.0.0</version> </dependency>
How to do it…
The Java implementation parses your JSON and exports a JsonPath
class with a method read that reads JSON, parses it, and then extracts the contents at the path you pass:
String json = "..."; List<String>titles = JsonPath.read(json, "$.store.book[*].title");
How it works…
The read...