0

I'm recieving JSON from the Google Directions API and the request is like the one shown here (under Directions Responses -> Json Output) link

I don't know how to extract a particular entry from within a json array. Basically what I need is the JsonArray equivalent of JSONObject method get(String key) where you can get the entry mapped to a specific key. However, the JSONArray has no such method, and seems to only support the retrieval of info through a specific index. This is all well and good, but the google directions reponses' contents can vary - so I need a way to either find a specific key-value pairing, or loop through the entire array and have a way to check the value of the key. I could even find a way to determine the key that a JSONObject was mapped to. Any suggestions? Ex: How would you extract the distance JSONObject out of the JSONArray legs?

Edit: This is where I am stuck: Refering the the JSON example output here... I am trying to get the value of the "overview_polyline" entry within the "routes" json array. Since I cannot access the "overview_polyline" entry simply by referring to its name, I must loop through the array until I get to that entry. But how would I know when I got to that entry? Yes, perhaps I could check if the JSONObject representation of the entry had a "points" key, but that isn't necessarily exclusive to the "overview_polyline" entry. Also, I cannot simply get the (array.length() - n) entry because the number of entries returned in the array may vary. In short, it seems to me that I need a way to check the name ("overview_polyline") of each JSONObject to reliably be able to extract that information.

            JSONArray routesArray = resultObj.getJSONArray("routes");

            for(int i = 0; i < routesArray.length(); i++)
            {
                JSONObject obj = routesArray.optJSONObject(i);
                if(obj != null) 
                {
                    //How do I determine if this object is the "overview_polyline" 
                    //object?
                }
            }
6
  • A JSONArray is an array, there is no key (only index) Commented Aug 23, 2014 at 19:57
  • Alright - thanks - but how would you find a specific element in the array when you know its name but its index changes? Commented Aug 23, 2014 at 21:17
  • if you have the name of the element, it's the element itself! Try to paste a real example. Commented Aug 23, 2014 at 21:18
  • @SimonMarquis I don't quite follow - please see my edit. Commented Aug 23, 2014 at 21:37
  • Unfortunately, JSON doesn't work like this... Paste the resultObj value: resultObj.toString() Commented Aug 23, 2014 at 21:39

1 Answer 1

2

The route is a JSONArray of JSONObject, so you have to do it this way:

JSONArray routesArray = resultObj.getJSONArray("routes");
for(int i = 0; i < routesArray.length(); i++) {
    JSONObject obj = routesArray.optJSONObject(i);
    if(obj != null) {
        JSONObject polyline = obj.optJSONObject("overview_polyline");
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh wow, thanks so much, that worked perfectly. I forgot that the overview_polyline was within a JSONObject. I was assuming it was in a JSONArray. Though if it was I'm assuming it would have been impossible to find. JSONArrays must be meant to only contain JSONObjects (or other JSONArrays) of the same type so that picking out one specific one is never needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.