Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing JSON from the google maps DistanceMatrix api in android

I am new to android development and new to JSON. I am using the google maps distance matrix api. I have the JSON download into a JSONObject properly I believe.(I stole the code to do it from another post). However I can not seem to parse the JSON properly. I have been working at this for a few day and am completely stumped. I make the following call to google below

https://maps.googleapis.com/maps/api/distancematrix/json?origins=1600%20pennsylvania%20avenue&destinations=1500%20college%20street&mode=driving&units=imperial

The output is this:

{
   "destination_addresses" : [ "1500 College Street, Beaumont, TX 77701, USA" ],
   "origin_addresses" : [ "1600 Pennsylvania Avenue, Hagerstown, MD 21742, USA" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "1,306 mi",
                  "value" : 2102536
               },
               "duration" : {
                  "text" : "18 hours 48 mins",
                  "value" : 67684
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

I have tried:

1.

JSONArray jsonArray = jObject.getJSONArray("rows");
JSONArray routes = jsonArray.getJSONArray(0);
stringBuilder.append(routes.getJSONObject(0).getString("text"));

2.

JSONArray jsonArray = jObject.getJSONArray("rows");
JSONObject routes = jsonArray.getJSONObject(0);
stringBuilder.append(routes.getJSONObject("distance").getString("text"));

3.

JSONArray jsonArray = jObject.getJSONArray("elements");                    stringBuilder.append(routes.getJSONObject(0).getString("text"));

I have tried more but those seem to me like they should work. It seemed that to me rows is an array and elements is an array as well. So it would follow that I would need to get rows out of the original JSONObject then get the element array out of the row array then get the distance object out of that array, then finally get the text value and add it to the string builder I created earlier. Were did I go wrong? thank you in advance for any help.

like image 409
eam1813 Avatar asked Apr 03 '15 21:04

eam1813


1 Answers

Here is what worked with me

//httpResponse is the output of google api
JSONObject jsonRespRouteDistance = new JSONObject(httpResponse)
                                        .getJSONArray("rows")
                                        .getJSONObject(0)
                                        .getJSONArray ("elements")
                                        .getJSONObject(0)
                                        .getJSONObject("distance");

String distance = jsonRespRouteDistance.get("text").toString();

/* 
* For distance, below is only partial solution as the 
* output to string destination_addr will contain square brackets [] and double codes ""
* Eg. [ "1600 Pennsylvania Avenue, Hagerstown, MD 21742, USA" ]
* 
*/
String destination_addr = new JSONObject(httpResponse)
                            .get("destination_addresses")
                            .toString();

[UPDATE]

Assuming we have only one destination address and not multiple. A little string manipulation gets us the clean string string without codes " and brackets []

StringBuilder stringBuilderDestinationAddr = new StringBuilder();

for (int i = 0; i < destination_addr.length(); i++)
    if (destination_addr.charAt(i) != '[' && destination_addr.charAt(i) != ']' && destination_addr.charAt(i) != '"')
         stringBuilderDestinationAddr.append(pickup_addr.destination_addr (i));

String strCleanDestination = stringBuilderDestinationAddr.toString();
like image 200
anubhav16 Avatar answered Nov 01 '22 22:11

anubhav16