出于与我正在从事的项目相关的原因,例如,我希望对 JSON 文件的整个查询作为字符串保存$.store.book[*].title
(而不是必须将文档的每个级别临时存储为单独的对象) .
我目前正在使用 JsonPath(版本 0.8.0,这是我能找到的最新版本),这基本上正是我正在寻找的,但我得到了下面显示的异常。我只是使用 JsonPath 谷歌代码页上给出的示例 JSON,使用他们的示例查询之一。
我在这里做错了什么?或者,如果没有解决方案,Java 中是否有替代 JsonPath 的方法?我希望能够将整个查询作为字符串传递,并且它必须使用 Java。
功能:
public void testJsonPath() throws Exception
{
String query = "$.store.book[*].title";
List toRet = (List) JsonPath.read(practiceJson, query, new Filter[0]);
System.out.println(toRet.toString());
}
例外:
java.lang.NoClassDefFoundError: net/minidev/json/parser/ParseException
at com.jayway.jsonpath.spi.JsonProviderFactory$1.create(JsonProviderFactory.java:27)
at com.jayway.jsonpath.spi.JsonProviderFactory.createProvider(JsonProviderFactory.java:32)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:202)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:307)
at net.windward.datasource.test.TestJsonDataSource.testJsonPath(TestJsonDataSource.java:119)
练习 JSON:
private String practiceJson = "{\n" +
" \"store\": {\n" +
" \"book\": [ {\n" +
" \"category\": \"reference\",\n" +
" \"author\": \"Nigel Rees\",\n" +
" \"title\": \"Sayings of the Century\",\n" +
" \"price\": 8.95\n" +
" }, {\n" +
" \"category\": \"fiction\",\n" +
" \"author\": \"Evelyn Waugh\",\n" +
" \"title\": \"Sword of Honour\",\n" +
" \"price\": 12.99\n" +
" }, {\n" +
" \"category\": \"fiction\",\n" +
" \"author\": \"Herman Melville\",\n" +
" \"title\": \"Moby Dick\",\n" +
" \"isbn\": \"0-553-21311-3\",\n" +
" \"price\": 8.99\n" +
" }, {\n" +
" \"category\": \"fiction\",\n" +
" \"author\": \"J. R. R. Tolkien\",\n" +
" \"title\": \"The Lord of the Rings\",\n" +
" \"isbn\": \"0-395-19395-8\",\n" +
" \"price\": 22.99\n" +
" } ],\n" +
" \"bicycle\": [ {\n" +
" \"color\": \"red\",\n" +
" \"price\": 19.95,\n" +
" \"style\": [ \"city\", \"hybrid\" ]\n" +
" }, {\n" +
" \"color\": \"blue\",\n" +
" \"price\": 59.91,\n" +
" \"style\": [ \"downhill\", \"freeride\" ]\n" +
" } ]\n" +
" }\n" +
"}";