Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?
                }
            }
share|improve this question
    
A JSONArray is an array, there is no key (only index) –  Simon Marquis Aug 23 '14 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? –  bw15 Aug 23 '14 at 21:17
    
if you have the name of the element, it's the element itself! Try to paste a real example. –  Simon Marquis Aug 23 '14 at 21:18
    
@SimonMarquis I don't quite follow - please see my edit. –  bw15 Aug 23 '14 at 21:37
    
Unfortunately, JSON doesn't work like this... Paste the resultObj value: resultObj.toString() –  Simon Marquis Aug 23 '14 at 21:39

1 Answer 1

up vote 0 down vote accepted

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");
    }
}
share|improve this answer
    
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. –  bw15 Aug 23 '14 at 22:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.