我有以下URI,它返回一个JSON响应
我试图解析get请求的响应并访问steps
数组,但它在第一步就停止了。
这是我的代码:
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>
答案 0 :(得分: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>