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.

4

1 回答 1

2

有一个类似的问题:JSON parsing of Google Maps API in Android App

我认为您应该首先使用索引从 JsonArray 获取一个对象。然后你应该得到腿作为数组。然后你应该从腿数组中得到一个对象。然后你可以得到steps数组。

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

于 2012-08-07T01:39:50.680 回答