-2

I have a jSON response from google directions API in the form of a string. How do I fetch the "maneuver" object from this? I want to place it inside another JSON array which I will later turn into a string.

Here is my json: https://maps.googleapis.com/maps/api/directions/json?origin=Disneyland&destination=Universal%20Studios%20Hollywood4&key=AIzaSyDeTp_6tFa2cUan9wUw3qgFi5-dm8xGRHs

Here is what I have done till now:

public JSONArray parseDirectionsForNavigation(String jsonData)
    {
        JSONArray jsonArray_live = null;
        JSONObject jsonObject_live;

        try {
            jsonObject_live = new JSONObject(jsonData);
            jsonArray_live = jsonObject_live.getJSONArray("routes").getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONArray("steps");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonArray_live;
    }

2 Answers2

1
JSONArray arr_routes = jsono.getJSONArray("routes");

JSONArray arr_legs = jsono.getJSONArray("legs");

JSONArray arr_steps = jsono.getJSONArray("steps");

for (int i = 0; i < arr_steps .length(); i++) {
                    JSONObject object = jarray.getJSONObject(i);

                    //create a string array of some sort.
                    String[] maneuvers = object.getString("maneuvers")

                }

You can try lot other ways of parsing but in getting inside you json can be done that way

That came from my own app too.

Orvenito
  • 437
  • 2
  • 14
  • jarray is the arr_steps yeah? – Shubham Jayswal Sep 28 '17 at 08:54
  • Thanks Overnito +1 for your answer. My points are less so cant do a +1. A last question if you don't mind. How do I display this string maneuver in android. I have all navigation direction and route and everything working. Do you think I can use this data to tell the user to move left and right as and when required? – Shubham Jayswal Sep 28 '17 at 09:11
  • If you wan't i shall post a different answer. because it's unfair to me if I answer it here. – Orvenito Sep 28 '17 at 09:58
0
  String maneuver[];
                            try {
                                JSONObject   json = new JSONObject(responseContent);
                                JSONArray json_Routes = json.getJSONArray("routes");
                                for (int i = 0; i < json_Routes.length(); i++) {
                                    JSONObject jsonRoute = json_Routes.getJSONObject(i);
                                    JSONArray jsonLegs = jsonRoute.getJSONArray("legs");
                                    for (int j = 0; j < jsonLegs.length(); j++) {
                                        JSONObject jsonsteps = jsonLegs.getJSONObject(j);
                                        JSONArray jsonmanr = jsonsteps.getJSONArray("steps");
                                        JSONArray Json_man=new JSONArray(String.valueOf(jsonmanr));
                                        maneuver=new String[Json_man.length()];
                                        for (int k =1; k < Json_man.length(); k++) {
                                            JSONObject jsonLeg = Json_man.getJSONObject(k);
                                            maneuver[k]=jsonLeg.getString("maneuver");
                                            Log.i("maneuver", maneuver[k]);

                                        }
                                    }
                                }
                            } catch (JSONException ew) {
                                ew.printStackTrace();
                            }
Ansal Rabin
  • 218
  • 1
  • 3
  • 13