1 回答
TA贡献1836条经验 获得超3个赞
UPD2: [表示数组,{在JSON语法中表示对象。试试。
private double getLat() throws IOException, JSONException {
JSONObject myData = readFromJsonUrl();
JSONArray results = myData.getJSONArray("results");
JSONObject geometry = results.getJSONObject(0).getJSONObject("geometry");
JSONObject location = geometry.getJSONObject("location");
double lat = location.getDouble("lat");
return lat;
}
UPD:您应该使用readLine()方法而不是read(),尝试更改您的代码,如下所示。
public String readAll(BufferedReader br) throws IOException {
StringBuilder sb = new StringBuilder();
String cp;
while ((cp = br.readLine()) != null) {
sb.append(cp);
}
return sb.toString();
}
您可以jsonText在控制台中打印以查看 JSON 语法是否正确。也许你可以尝试使用这段代码。
private JSONObject readFromJsonUrl() throws IOException, JSONException {
InputStream is = new URL(this.fullUrl).openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(br);
//jsonObject = new JSONObject(jsonText);
ObjectMapper objectMapper = new ObjectMapper();
JSONObject jsonObject = objectMapper.readValue(jsonText, JSONObject.class);
return jsonObject;
}
添加回答
举报