1 回答

TA貢獻(xiàn)1836條經(jīng)驗 獲得超3個贊
UPD2: [表示數(shù)組,{在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:您應(yīng)該使用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;
}
添加回答
舉報