无法解析 google 个位置 JSON 响应 - org.json.JSONException
Unable to parse google places JSON response - org.json.JSONException
我正在尝试解析来自 Google 个地方 API 的 JSON 响应。但是我得到 org.json.JSONException.
这是我的 JSON 回复。
我正在使用以下代码来检索格式化地址。
try {
Log.e("test-ttt", jsonResults.toString());
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(jsonResults.toString());
JSONObject placeDetailsJsonArray = jsonObj.getJSONObject("result");
// Extract the Place descriptions from the results
placeDetails = "NAME: " + placeDetailsJsonArray.getJSONObject("name").toString();
placeDetails += "ADDRESS: " + placeDetailsJsonArray.getJSONObject("formatted_address").toString();
} catch (JSONException e) {
Log.e(TAG, "Cannot process JSON results", e);
}
这是我得到的 logcat 异常:
org.json.JSONException: Value South Point School at name of type java.lang.String cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:100)
at org.json.JSONObject.getJSONObject(JSONObject.java:578)
at manasthemarvel.triptimeline.PlaceAPI.getPlaceDetails(PlaceAPI.java:78)
at manasthemarvel.triptimeline.placeInfo.doInBackground(placeInfo.java:14)
at manasthemarvel.triptimeline.placeInfo.doInBackground(placeInfo.java:10)
at android.os.AsyncTask.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
// Extract the Place descriptions from the results
placeDetails = "NAME: " + placeDetailsJsonArray.getJSONObject("name").toString();
placeDetails += "ADDRESS: " + placeDetailsJsonArray.getJSONObject("formatted_address").toString();
我在这里做错了什么?
我不知道 jsonResults
是什么,但最有可能在这里做 jsonResults.toString()
:
JSONObject jsonObj = new JSONObject(jsonResults.toString());
未提供有效的 JSON 字符串。您应该检查正在处理的内容(执行 Log.d("X", jsonResults.toString());
),而不是显示远程 API 生成的内容。
您将 "name" 和 "formatted_address" 视为 JSONObject 而不是正常的 key/value 对。
试试这个:
JSONObject placeDetailsJsonArray = jsonObj.getJSONObject("result");
String name = placeDetailsJsonArray.getString("name");
name 和 formatted_address 都是 JSON 中的字符串,但您试图将其获取为 JsonObject。而是使用类似
的东西
placeDetails = "NAME: " + placeDetailsJsonArray.get("name").getAsString();
placeDetails += "ADDRESS: " + placeDetailsJsonArray.get("formatted_address").getAsString();
我正在尝试解析来自 Google 个地方 API 的 JSON 响应。但是我得到 org.json.JSONException.
这是我的 JSON 回复。
我正在使用以下代码来检索格式化地址。
try {
Log.e("test-ttt", jsonResults.toString());
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(jsonResults.toString());
JSONObject placeDetailsJsonArray = jsonObj.getJSONObject("result");
// Extract the Place descriptions from the results
placeDetails = "NAME: " + placeDetailsJsonArray.getJSONObject("name").toString();
placeDetails += "ADDRESS: " + placeDetailsJsonArray.getJSONObject("formatted_address").toString();
} catch (JSONException e) {
Log.e(TAG, "Cannot process JSON results", e);
}
这是我得到的 logcat 异常:
org.json.JSONException: Value South Point School at name of type java.lang.String cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:100)
at org.json.JSONObject.getJSONObject(JSONObject.java:578)
at manasthemarvel.triptimeline.PlaceAPI.getPlaceDetails(PlaceAPI.java:78)
at manasthemarvel.triptimeline.placeInfo.doInBackground(placeInfo.java:14)
at manasthemarvel.triptimeline.placeInfo.doInBackground(placeInfo.java:10)
at android.os.AsyncTask.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
// Extract the Place descriptions from the results
placeDetails = "NAME: " + placeDetailsJsonArray.getJSONObject("name").toString();
placeDetails += "ADDRESS: " + placeDetailsJsonArray.getJSONObject("formatted_address").toString();
我在这里做错了什么?
我不知道 jsonResults
是什么,但最有可能在这里做 jsonResults.toString()
:
JSONObject jsonObj = new JSONObject(jsonResults.toString());
未提供有效的 JSON 字符串。您应该检查正在处理的内容(执行 Log.d("X", jsonResults.toString());
),而不是显示远程 API 生成的内容。
您将 "name" 和 "formatted_address" 视为 JSONObject 而不是正常的 key/value 对。
试试这个:
JSONObject placeDetailsJsonArray = jsonObj.getJSONObject("result");
String name = placeDetailsJsonArray.getString("name");
name 和 formatted_address 都是 JSON 中的字符串,但您试图将其获取为 JsonObject。而是使用类似
的东西placeDetails = "NAME: " + placeDetailsJsonArray.get("name").getAsString();
placeDetails += "ADDRESS: " + placeDetailsJsonArray.get("formatted_address").getAsString();