B. Money
B. Money

Reputation: 941

How do i parse Google Places Detail JSON file?

I am a noob to android and i want to parse a google response JSON with the same formatting as the example here: https://developers.google.com/places/documentation/details. I'm trying to get the string formatted phone number. However, when i code to .getString from the array "events" my editor asks for a int index. I don't understand what is going on. Any help is greatly appreciated. Here is my code:

try {   
        JSONObject googleObject = StoresNearMe.getJSONfromURL(googleplacedetailapi);
        JSONObject parsedGoogle = googleObject.getJSONObject("results");
        JSONArray  parsedevents = parsedGoogle.getJSONArray("events");
        //thing.setText(googleplacesapi);
        if(parsedevents.equals("") ){
            Toast.makeText(FindDealer.this, "No nearby locations", Toast.LENGTH_LONG).show();
        }else{
            String phonenumber = parsedevents.getString("formatted_phone_number");
            thing.setText(phonenumber);   
      }            
  } catch (JSONException e) {
      Log.d("log_tag","JSON parsing error - Google Places Api:" + e.getMessage());
  }      

Upvotes: 0

Views: 822

Answers (1)

pawelzieba
pawelzieba

Reputation: 16082

formatted_phone_number is in results json object, not in events json array.

Change the line:

String phonenumber = parsedevents.getString("formatted_phone_number");

to

String phonenumber = parsedGoogle.getString("formatted_phone_number");

You might read something about json structure on json.org.

Upvotes: 1

Related Questions