简体   繁体   中英

Unable to parse google places JSON response - org.json.JSONException

I am receiving a JSON response from Google places API which I am trying to parse. But I am getting org.json.JSONException.

This is my JSON response.

https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJbf6hrtZ2AjoRwUPd_nrhVjM&key=AIzaSyBWKQHS39-SYUNxEEAry1FxrMET2NwhqxE

I am using the following code to retrieve the formatted address.

  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);
    }

this is the logcat exception I am getting:

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$2.call(AsyncTask.java:288)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor$1.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();

What am I doing wrong here?

You're treating the "name" and the "formatted_address" as a JSONObject instead of a normal key/value pair.

Try this:

JSONObject placeDetailsJsonArray = jsonObj.getJSONObject("result");
String name = placeDetailsJsonArray.getString("name");

I do not know what jsonResults is, but most likely doing jsonResults.toString() here:

JSONObject jsonObj = new JSONObject(jsonResults.toString());

is not giving valid JSON string. You should check what you are processing (do Log.d("X", jsonResults.toString()); ) instead of showing what remote API produces.

name and formatted_address are both String in the JSON but you are trying to get it as JsonObject. Instead use something like

placeDetails = "NAME: " + placeDetailsJsonArray.get("name").getAsString();
placeDetails += "ADDRESS: " + placeDetailsJsonArray.get("formatted_address").getAsString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM