0

I want to parse the JSON file at this link: http://maps.googleapis.com/maps/api/directions/json?origin=33.7489,-84.3881&destination=33.7514,-84.7478&sensor=false

I want to parse the "html_instructions" string, from the "steps" array as such:

String googledirectionsapi = "http://maps.googleapis.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination0 + "&sensor=false";                     
                    try {   
                        JSONObject googleObject = Directions.getJSONfromURL(googledirectionsapi);
                        JSONArray parsedGoogleDirections = googleObject.getJSONArray("routes").getJSONArray("steps");       
                        thing.setText(googledirectionsapi);
                        if(parsedGoogleDirections.equals("") ){
                            Toast.makeText(FindDealer.this, "Cannot find suitable route", Toast.LENGTH_LONG).show();
                        }else{                           
                             for (int i = 0; i < parsedGoogleDirections.length(); i++) {
                                    JSONObject instructions = parsedGoogleDirections.getJSONObject(i);              
                                    if (i == 0){
                                        String step1 = parsedGoogleDirections.getString("html_instructions");

                             }
                             }
                             }
                    }catch (JSONException e) {
                              Log.d("log_tag","JSON parsing error - Google Directions Api:" + e.getMessage());
                        }   

However, eclipse is giving me errors at:

.getJSONArray("steps");  //<-- The method getJSONArray(int) in the type JSONArray is not applicable for the arguments (String).

and and also giving me errors at:

.getString("html_instructions"); //<--he method getString(int) in the type JSONArray is not applicable for the arguments (String)

Why is eclipse showing these errors? I've never had this issue before. THank you in advance.

B. Money
  • 931
  • 2
  • 19
  • 56

1 Answers1

2

There is a similar question:JSON parsing of Google Maps API in Android App

I think you should firstly get an Object from JsonArray using index. Then you should get legs as array.Then you should get an object from leg array. then you can get steps array.

https://stackoverflow.com/a/7237305/538408

Community
  • 1
  • 1
Murat
  • 3,084
  • 37
  • 55