Java 如何解析JSON REST响应-NoClassDefFoundError:org/JSON/JSONArray
我有以下URI,它返回一个JSON响应。 我试图解析get请求的响应并访问steps数组,但在第一步就停止了 这是我的密码:Java 如何解析JSON REST响应-NoClassDefFoundError:org/JSON/JSONArray,java,json,Java,Json,我有以下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";
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"));
}
}
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>
org/json/JSONArray
您编译了错误的库和/或导入了错误的类
替换
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
与
看起来您在类路径中丢失了jar文件。错误只是说您缺少运行时依赖项。您需要在类路径中添加json.jar
<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>