I need to get the data from a JSON obtained through the API of Google Directions .
I have this code, which apparently is correct:
List<Route> routes = new ArrayList<Route>();
JSONObject jsonData = new JSONObject(data);
JSONArray jsonRoutes = jsonData.getJSONArray("routes");
for (int i = 0; i < jsonRoutes.length(); i++) {
JSONObject jsonRoute = jsonRoutes.getJSONObject(i);
Route route = new Route();
JSONObject overview_polylineJson = jsonRoute.getJSONObject("overview_polyline");
JSONArray jsonLegs = jsonRoute.getJSONArray("legs");
JSONObject jsonLeg = jsonLegs.getJSONObject(0);
JSONObject jsonDistance = jsonLeg.getJSONObject("distance");
JSONObject jsonDuration = jsonLeg.getJSONObject("duration");
JSONObject jsonEndLocation = jsonLeg.getJSONObject("end_location");
JSONObject jsonStartLocation = jsonLeg.getJSONObject("start_location");
JSONArray
jsonInstruction=jsonLeg.getJSONArray("html_instructions");
}
How can I save the values? I'll put a sample JSON so you can guide me:
{
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "ChIJNZzlDPLWYpYRCJBxIkFwiD0",
"types" : [ "street_address" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJheKsagfQYpYRI6X_s8MFSO4",
"types" : [ "route" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : -33.4907394,
"lng" : -70.5542248
},
"southwest" : {
"lat" : -33.592532,
"lng" : -70.6199773
}
},
"copyrights" : "Datos del mapa ©2017 Google",
"legs" : [
{
"distance" : {
"text" : "13.9 km",
"value" : 13892
},
"duration" : {
"text" : "31 min",
"value" : 1842
},
"end_address" : "Alcalde Pedro Alarcón, San Joaquín, Región Metropolitana, Chile",
"end_location" : {
"lat" : -33.4907394,
"lng" : -70.6199773
},
"start_address" : "Auriga 2044, Puente Alto, Región Metropolitana, Chile",
"start_location" : {
"lat" : -33.592532,
"lng" : -70.5548994
},
"steps" : [
{
"distance" : {
"text" : "80 m",
"value" : 80
},
"duration" : {
"text" : "1 min",
"value" : 9
},
"end_location" : {
"lat" : -33.5919623,
"lng" : -70.5543792
},
"html_instructions" : "Dirígete al \u003cb\u003enordeste\u003c/b\u003e por \u003cb\u003eAuriga\u003c/b\u003e hacia \u003cb\u003eAries\u003c/b\u003e.\u003cdiv style=\"font-size:0.9em\"\u003eCalle de uso restringido\u003c/div\u003e",
"polyline" : {
"points" : "h''lEbgsmLqBgB"
},
"start_location" : {
"lat" : -33.592532,
"lng" : -70.5548994
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "11 m",
"value" : 11
},
"duration" : {
"text" : "1 min",
"value" : 10
},
"end_location" : {
"lat" : -33.5918992,
"lng" : -70.5544702
},
"html_instructions" : "Gira a la \u003cb\u003eizquierda\u003c/b\u003e con dirección a \u003cb\u003eAries\u003c/b\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "v|_lEzcsmLKP"
},
"start_location" : {
"lat" : -33.5919623,
"lng" : -70.5543792
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "39 m",
"value" : 39
},
"duration" : {
"text" : "1 min",
"value" : 14
},
"end_location" : {
"lat" : -33.5916156,
"lng" : -70.5542248
},
"html_instructions" : "Gira a la \u003cb\u003ederecha\u003c/b\u003e con dirección a \u003cb\u003eCentral\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "j|_lEldsmLw@q@"
},
"start_location" : {
"lat" : -33.5918992,
"lng" : -70.5544702
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.3 km",
"value" : 344
},
"duration" : {
"text" : "1 min",
"value" : 51
},
"end_location" : {
"lat" : -33.589296,
"lng" : -70.5565952
},
"html_instructions" : "Gira a la \u003cb\u003eizquierda\u003c/b\u003e con dirección a \u003cb\u003eAv. Troncal San Francisco\u003c/b\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "rz_lEzbsmLIL}@|AGLKLQTKLYVUPy@d@iAl@k@Xg@t@mA'B"
},
"start_location" : {
"lat" : -33.5916156,
"lng" : -70.5542248
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "47 m",
"value" : 47
},
"duration" : {
"text" : "1 min",
"value" : 11
},
"end_location" : {
"lat" : -33.5888809,
"lng" : -70.5566925
},
"html_instructions" : "Continúa recto",
"maneuver" : "straight",
"polyline" : {
"points" : "bl_lEvqsmLsAP"
},
"start_location" : {
"lat" : -33.589296,
"lng" : -70.5565952
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "2.2 km",
"value" : 2236
},
"duration" : {
"text" : "6 min",
"value" : 330
},
"end_location" : {
"lat" : -33.5768974,
"lng" : -70.5755764
},
"html_instructions" : "Gira a la \u003cb\u003eizquierda\u003c/b\u003e con dirección a \u003cb\u003eAv. Troncal San Francisco\u003c/b\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "ni_lEhrsmL]r@[z@GZCHCFELINEHGFSRYTe@'@KJYZSXWf@MZGNGNGTGPCLCLU'B['Co@rFGd@EPELMXYn@eBfCU\UXSXcAzA{AtBaApAEFqAvAGDm@n@GFYZu@r@k@f@u@r@UXuAjCKR}@bBaBbDmCdFg@dAQ'@Yx@ITWz@Sb@q@dAm@x@k@v@k@x@g@p@q@bAILGHeCtDuBtCe@p@s@nA"
},
"start_location" : {
"lat" : -33.5888809,
"lng" : -70.5566925
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.8 km",
"value" : 835
},
"duration" : {
"text" : "2 min",
"value" : 124
},
"end_location" : {
"lat" : -33.5740375,
"lng" : -70.5826933
},
"html_instructions" : "Gira levemente a la \u003cb\u003eizquierda\u003c/b\u003e con dirección a \u003cb\u003eGabriela Ote\u003c/b\u003e",
"maneuver" : "turn-slight-left",
"polyline" : {
"points" : "r~|kEjhwmLUdAOt@O'@K'@EVAJ@L[hB]|AI'@Y|AYxAGZYzAWvAERGRGLGHQHQDQ@K@mABe@@A?O@WBQBKDQHUPQXEJGNAHCHAJAJAX?PHfATfCBZ@BFd@"
},
"start_location" : {
"lat" : -33.5768974,
"lng" : -70.5755764
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.7 km",
"value" : 698
},
"duration" : {
"text" : "2 min",
"value" : 101
},
"end_location" : {
"lat" : -33.5678508,
"lng" : -70.58396239999999
},
"html_instructions" : "Gira a la \u003cb\u003ederecha\u003c/b\u003e con dirección a \u003cb\u003eAv. Concha Y Toro\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "vl|kExtxmLgBRkC^m@HWDk@F]D_ALe@DUDYB]DsFn@oALa@FWBqAPKBaEf@"
},
"start_location" : {
"lat" : -33.5740375,
"lng" : -70.5826933
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "5.4 km",
"value" : 5433
},
"duration" : {
"text" : "10 min",
"value" : 603
},
"end_location" : {
"lat" : -33.5209376,
"lng" : -70.5990908
},
"html_instructions" : "Continúa por \u003cb\u003eAv. Vicuña Mackenna\u003c/b\u003e/\u003cb\u003eAv. Vicuña Mackenna Ote.\u003c/b\u003e",
"polyline" : {
"points" : "'f{kEv|xmLmALkBLeD\o@JwFh@kBHe@DOBaIz@SBw@De@DgBJ}@LM@mBVsANSBg@FmD'@aBPwAJeAHMBy@F_AHUB_BNgEl@oEZQBoBTUDsAJaBRuGp@k@FkBRwFb@mDXuD^WDUHKDa@N]Nq@XoBp@oAb@KBiDhAo@PoCx@KDi@R]LqEfBq@RgBn@sAd@iA^{C~@oXnJ{JhDcFfBuIxCk@Py@RoCv@yAb@cCt@SLmJtCKBOF}Bv@_Ct@mBx@a@NeA^oA^C@oAb@[Hs@Pa@Lk@R[H{@Vo@RWHIBQFKD]Ng@VUJKF"
},
"start_location" : {
"lat" : -33.5678508,
"lng" : -70.58396239999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "3.3 km",
"value" : 3317
},
"duration" : {
"text" : "7 min",
"value" : 401
},
"end_location" : {
"lat" : -33.4961257,
"lng" : -70.6164586
},
"html_instructions" : "Continúa recto por \u003cb\u003eAv. Vicuña Mackenna\u003c/b\u003e/\u003cb\u003eAv. Vicuña Mackenna Ote.\u003c/b\u003e",
"maneuver" : "straight",
"polyline" : {
"points" : "z'rkEh{{mLs@b@m@\a@TwE~Ck@'@sBrAk@^iAr@KFqBvA{@j@_BfA_@^SR_@b@i@p@QTcBpBgAxAKLqBlCY'@ORq@|@a@j@o@x@sAbBKLW\{@bAmDvEMPy@dAq@t@a@l@eArAg@n@[^mA~Ag@r@_BvBaAtA}@fA['@gDhFi@l@GFWVSLSJm@N{@Lw@L_@FoANsCb@UDeCb@MBI?wC'@iJjAQBQDYBmMxAUDM@cDj@eAPODyBb@yAR"
},
"start_location" : {
"lat" : -33.5209376,
"lng" : -70.5990908
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.2 km",
"value" : 167
},
"duration" : {
"text" : "1 min",
"value" : 47
},
"end_location" : {
"lat" : -33.4962922,
"lng" : -70.6180809
},
"html_instructions" : "Gira a la \u003cb\u003eizquierda\u003c/b\u003e con dirección a \u003cb\u003eAv. Salvador Allende\u003c/b\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "xemkEzg_nLK@O@D\Lp@f@nF"
},
"start_location" : {
"lat" : -33.4961257,
"lng" : -70.6164586
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.5 km",
"value" : 506
},
"duration" : {
"text" : "1 min",
"value" : 87
},
"end_location" : {
"lat" : -33.4918286,
"lng" : -70.61912989999999
},
"html_instructions" : "Gira a la \u003cb\u003ederecha\u003c/b\u003e con dirección a \u003cb\u003eSan Juan\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "xfmkE~q_nLe@FkC\gBVa@FM@mATUBkCb@MByCb@_BTy@L"
},
"start_location" : {
"lat" : -33.4962922,
"lng" : -70.6180809
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "59 m",
"value" : 59
},
"duration" : {
"text" : "1 min",
"value" : 12
},
"end_location" : {
"lat" : -33.491807,
"lng" : -70.6197696
},
"html_instructions" : "Gira a la \u003cb\u003eizquierda\u003c/b\u003e con dirección a \u003cb\u003eEl Pinar\u003c/b\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "|jlkEpx_nLCvA?f@"
},
"start_location" : {
"lat" : -33.4918286,
"lng" : -70.61912989999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.1 km",
"value" : 120
},
"duration" : {
"text" : "1 min",
"value" : 42
},
"end_location" : {
"lat" : -33.4907394,
"lng" : -70.6199773
},
"html_instructions" : "Gira a la \u003cb\u003ederecha\u003c/b\u003e con dirección a \u003cb\u003eAlcalde Pedro Alarcón\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eEl destino está a la derecha.\u003c/div\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "xjlkEp|_nLe@B_BRoAP"
},
"start_location" : {
"lat" : -33.491807,
"lng" : -70.6197696
},
"travel_mode" : "DRIVING"
}
],
"traffic_speed_entry" : [],
"via_waypoint" : []
}
],
"overview_polyline" : {
"points" : "h''lEbgsmLqBgBKPw@q@ILeAjB]b@e@d@oAv@uBfAuBvCsAP]r@c@vAWn@MPm@h@q@l@m@t@e@bAO^WbAq@bFw@xGK^g@hAeDxE_DpEgAxAoCtCoAnAaBzAUXuAjCiAvBoFhKy@fBoAnDwDpFkBlC{FjIyA'Ce@zB[bAGb@@L[hBg@~Bs@vD_AbFO'@YRc@FyADoAF]Hg@ZWd@IXETCd@HxAXbDHh@sFr@oC\uC\cKjAwH'AyDZuEh@wFh@kBHu@HuI~@}AJgBJ}@L{BX}H|@mGj@oFd@gEl@oEZaCXaNvAwCZeL|@mEd@a@N_A^aDjAuHbC{C~@gA'@cGzB{DtAeF~Akd@xOyP'GeBd@iFzAcCt@SLyJxCmC~@_Ct@mBx@gBn@sA'@kBl@}C|@mCx@]LeAf@cCtAyKjHuBrA}B~A{CrBs@r@_E|E_FvGsCvDsDrE{DhFkBzBgB'CcAnAwG'JyAhBgDhFi@l@_@^g@Xm@N{@LwATcFr@{Ch@yOpBkOfBmGdAiCh@eBTO@D\Lp@f@nFqDd@iC^}F~@aIjAC~BeCVoAP"
},
"summary" : "Av. Vicuña Mackenna/Av. Vicuña Mackenna Ote.",
"warnings" : [],
"waypoint_order" : []
}
],
"status" : "OK"
}
To make everything clearer, I must obtain the travel distance, points of the trip, travel time and travel instructions. How to do it?