2013-07-18 40 views
7

由於與我正在處理的項目有關的原因,我希望將整個查詢保存爲JSON文件作爲字符串,例如$.store.book[*].title(而不是必須將文檔的每個級別臨時存儲爲一個單獨的對象)。JsonPath NoClassDefFoundError,或Java中JsonPath的替代者

我目前正在使用JsonPath(版本0.8.0,這是我能找到的最新版本),這基本上就是我在找的東西,但是我得到如下所示的異常。我只是使用JsonPath google代碼頁上給出的示例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" + 
     "}"; 

回答

8

如果您正在使用Maven的搖籃,只需要添加到您的依賴列表:

對於Maven項目:

在你的Maven的pom.xml文件:

<dependency> 
    <groupId>com.jayway.jsonpath</groupId> 
    <artifactId>json-path</artifactId> 
    <version>2.2.0</version> 
    <scope>test</scope> 
</dependency> 

對於Gradle項目:

依賴於你的搖籃的build.gradle文件

testCompile 'com.jayway.jsonpath:json-path:2.2.0' 

部分它應該工作

+0

我不知道爲什麼它不適用於我。如果我檢查依賴關係,我可以看到jsonPath正確地添加到測試中,但我總是有這個異常。 –

0

當是json-smart衝突的版本中,如果舊版本選取了哪些呢沒有預期的課程。

如何解決

驗證版本的mvn dependency:tree

下面,有net.minidev:json-smart:jar:1.1.1:compilecom.jayway.jsonpath:json-path:jar:1.1.0:test它使用最新版本json-smart

$ mvn clean dependency:tree | grep json 
[INFO] +- net.logstash.log4j:jsonevent-layout:jar:1.7:compile 
[INFO] | +- net.minidev:json-smart:jar:1.1.1:compile 
[INFO] | +- io.spray:spray-json_2.11:jar:1.3.3:compile 
[INFO] | +- org.json:json:jar:20160810:compile 
[INFO] +- com.github.fge:json-schema-validator:jar:2.2.6:compile 
[INFO] | +- com.github.fge:json-schema-core:jar:1.2.5:compile 
[INFO] | | +- org.skyscreamer:jsonassert:jar:1.4.0:test 
[INFO] | | | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test 
[INFO] +- com.jayway.jsonpath:json-path:jar:1.1.0:test 
[INFO] | | | | +- io.gatling:jsonpath_2.10:jar:0.6.1:test 
[INFO] | | | | +- io.fastjson:boon:jar:0.28:test 

所以,從依賴排除json-smart舊版本爲json-path有最新版本的json-smart

例如。

<dependency> 
     <groupId>net.logstash.log4j</groupId> 
     <artifactId>jsonevent-layout</artifactId> 
     <version>1.7</version> 
     <scope>compile</scope> 
     <exclusions> 
      <exclusion> 
       <groupId>net.minidev</groupId> 
       <artifactId>json-smart</artifactId> 
      </exclusion> 
     </exclusions> 
    </dependency>