我有以下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
专家是:
<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>
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>