i have the following JSON response from google maps. I’ve given two postcodes as the start and destination. It describes the route in the response so that part is working. I’d like to get the longitude and latitude from the response so that i can load the respective map. The problem i’m having is that “northeast” attribute cannot be found, any ideas?
02-05 18:50:03.668: E/*********HelloviewActivity(10381): response code OK
02-05 18:50:03.673: E/*********HelloviewActivity(10381): {
02-05 18:50:03.673: E/*********HelloviewActivity(10381): "routes" : [
02-05 18:50:03.673: E/*********HelloviewActivity(10381): {
02-05 18:50:03.673: E/*********HelloviewActivity(10381): "bounds" : {
02-05 18:50:03.673: E/*********HelloviewActivity(10381): "northeast" : {
02-05 18:50:03.678: E/*********HelloviewActivity(10381): "lat" : 53.654430,
02-05 18:50:03.678: E/*********HelloviewActivity(10381): "lng" : -1.778250
02-05 18:50:03.678: E/*********HelloviewActivity(10381): },
02-05 18:50:03.678: E/*********HelloviewActivity(10381): "southwest" : {
02-05 18:50:03.683: E/*********HelloviewActivity(10381): "lat" : 53.62041000000001,
02-05 18:50:03.683: E/*********HelloviewActivity(10381): "lng" : -1.831710
02-05 18:50:03.683: E/*********HelloviewActivity(10381): }
02-05 18:50:03.683: E/*********HelloviewActivity(10381): },
.
String jsonOutput = response.toString();
// Log.e(TAG,"jsonOutput = " + jsonOutput);
JSONObject results = null;
try {
results = new JSONObject(jsonOutput);
routes = results.getJSONArray("routes");
bounds = routes.getJSONObject(0);
northeast = bounds.getJSONObject("northeast");
lat = Double.parseDouble((String) northeast.get("lat"));
lon = Double.parseDouble((String) northeast.get("lng"));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e(TAG, "lon/lat = "+lon +" "+lat);
.
02-05 18:50:04.668: W/System.err(10381): org.json.JSONException: JSONObject["northeast"] not found.
routesis an array of (anonymous) objects that contain theboundsobject.Edit: Also – once you get your
boundsobject;latandlngaren’tStrings – they’re alreadyDoubles – you don’t do any conversion: