2017-04-14 77 views
-2

我有以下的URI,它返回給我一個JSON響應。
我試圖解析獲取請求的響應並訪問steps數組,但它在第一步中停止。如何解析JSON REST響應 - NoClassDefFoundError:org/json/JSONArray

這裏是我的代碼:

public void getRoute() throws Exception { 

     String urlToRead="https://maps.googleapis.com/maps/api/directions/json?origin=41.43206,-81.38992&destination=Montreal&key=XXX"; 

     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpGet getRequest = new HttpGet(urlToRead); 
     getRequest.addHeader("accept", "application/json"); 

     HttpResponse response = httpClient.execute(getRequest); 
     String json = IOUtils.toString(response.getEntity().getContent()); 


     JSONArray array = new JSONArray(json); 
     for (int i = 0; i < array.length(); i++) { 
      JSONObject object = array.getJSONObject(i); 
      System.out.println(object.getJSONArray("routes")); 
     } 
    } 

,但它與抱怨:

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONArray 

Maven的是:

<groupId>com.googlecode.json-simple</groupId> 
    <artifactId>json-simple</artifactId> 
    <version>1.1.1</version> 
</dependency> 
<dependency> 
    <groupId>org.apache.httpcomponents</groupId> 
    <artifactId>httpclient</artifactId> 
    <version>4.1.1</version> 
</dependency> 
+1

好像你已經錯過了jar文件在類路徑 – Arvind

+1

的錯誤只是說你缺少運行時的依賴。你需要在類路徑中添加'json.jar' –

回答

0

org/json/JSONArray

您編譯錯誤的庫和/或導入錯誤的類。

更換

<groupId>com.googlecode.json-simple</groupId> 
<artifactId>json-simple</artifactId> 
<version>1.1.1</version> 

隨着

<groupId>org.json</groupId> 
<artifactId>json</artifactId> 
<version>20160810</version>