我想获取所有html指令数据(来自路线> leg>步骤> html_instruction)。
我在这段代码中得到一个null返回。我不知道如何解决这个问题。有人可以教我如何解决它吗?
String final_json = buffer.toString();
JSONObject parent_object = new JSONObject(final_json);
JSONArray parent_array_routes = parent_object.getJSONArray("routes");
JSONArray legs = parent_array_routes.getJSONArray(2);
JSONArray steps = legs.getJSONArray(8);
StringBuffer html_instructions = new StringBuffer();
for(int i = 0; i<steps.length();i++){
JSONObject htm = steps.getJSONObject(i);
html_instructions.append(htm.getString("html_instructions") + "\n");
}
JSON
Response
{
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "ChIJyYfhZ79ZwokRMtXcL6CYxkA",
"types" : [ "premise" ]
},
{
"geocoder_status" : "OK",
"partial_match" : true,
"place_id" : "ChIJ8YWMWnz4wokRCOVf1CcJCbY",
"types" : [ "street_address" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 40.8171321,
"lng" : -73.99449150000001
},
"southwest" : {
"lat" : 40.7416627,
"lng" : -74.0728354
}
},
"copyrights" : "Map data ©2015 Google",
"legs" : [
{
"distance" : {
"text" : "9.7 mi",
"value" : 15653
},
"duration" : {
"text" : "25 mins",
"value" : 1480
},
"end_address" : "1 MetLife Stadium Dr, East Rutherford, NJ 07073, USA",
"end_location" : {
"lat" : 40.814505,
"lng" : -74.07272910000002
},
"start_address" : "75 Ninth Ave, New York, NY 10011, USA",
"start_location" : {
"lat" : 40.7428759,
"lng" : -74.00584719999999
},
"steps" : [
{
"distance" : {
"text" : "440 ft",
"value" : 134
},
"duration" : {
"text" : "1 min",
"value" : 34
},
"end_location" : {
"lat" : 40.7422925,
"lng" : -74.004457
},
"html_instructions" : "Head \u003cb\u003esoutheast\u003c/b\u003e on \u003cb\u003eW 16th St\u003c/b\u003e toward \u003cb\u003eNinth Ave\u003c/b\u003e",
"polyline" : {
"points" : "_rtwFpgubMtBuG"
},
"start_location" : {
"lat" : 40.7428759,
"lng" : -74.00584719999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "49 ft",
"value" : 15
},
"duration" : {
"text" : "1 min",
"value" : 29
},
"end_location" : {
"lat" : 40.7421744,
"lng" : -74.0045361
},
"html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e at the 1st cross street onto \u003cb\u003eNinth Ave\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "intwFz~tbMVN"
},
"start_location" : {
"lat" : 40.7422925,
"lng" : -74.004457
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "226 ft",
"value" : 69
},
"duration" : {
"text" : "1 min",
"value" : 24
},
"end_location" : {
"lat" : 40.7416627,
"lng" : -74.0049708
},
"html_instructions" : "Slight \u003cb\u003eright\u003c/b\u003e to stay on \u003cb\u003eNinth Ave\u003c/b\u003e",
"maneuver" : "turn-slight-right",
"polyline" : {
"points" : "qmtwFj_ubMDN@@?@JFXP`@VTN"
},
"start_location" : {
"lat" : 40.7421744,
"lng" : -74.0045361
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.2 mi",
"value" : 266
},
"duration" : {
"text" : "1 min",
"value" : 74
},
"end_location" : {
"lat" : 40.74282729999999,
"lng" : -74.00773459999999
},
"html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eW 15th St\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "kjtwF`bubMIVeBlFEN{A|EWr@"
},
"start_location" : {
"lat" : 40.7416627,
"lng" : -74.0049708
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "1.1 mi",
"value" : 1815
},
"duration" : {
"text" : "6 mins",
"value" : 349
},
"end_location" : {
"lat" : 40.7571135,
"lng" : -73.9973176
},
"html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e at the 1st cross street onto \u003cb\u003e10th Ave\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "uqtwFhsubMo@c@oA{@iAw@A?EEc@YwBuAaAq@o@c@gBiAMIsBoAmA{@m@a@eCaBcCaB}BuA{ByAyB{A{ByAyByA_C{AYU}AaA{ByAUMCC]UcAs@eC_BeC_B{ByA{ByAyByA"
},
"start_location" : {
"lat" : 40.74282729999999,
"lng" : -74.00773459999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.2 mi",
"value" : 273
},
"duration" : {
"text" : "1 min",
"value" : 59
},
"end_location" : {
"lat" : 40.7559092,
"lng" : -73.99449150000001
},
"html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eW 38th St\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "}jwwFfrsbMzA}E@CNe@Tu@d@uAHYHU@AHWDM^iA"
},
"start_location" : {
"lat" : 40.7571135,
"lng" : -73.9973176
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "377 ft",
"value" : 115
},
"duration" : {
"text" : "1 min",
"value" : 38
},
"end_location" : {
"lat" : 40.7550018,
"lng" : -73.9951569
},
"html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e at the 1st cross street onto \u003cb\u003eNinth Ave\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "mcwwFp`sbMtBtA~@n@"
},
"start_location" : {
"lat" : 40.7559092,
"lng" : -73.99449150000001
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.1 mi",
"value" : 226
},
"duration" : {
"text" : "1 min",
"value" : 39
},
"end_location" : {
"lat" : 40.7560886,
"lng" : -73.9950563
},
"html_instructions" : "Slight \u003cb\u003eright\u003c/b\u003e onto the \u003cb\u003eLincoln Tunnel\u003c/b\u003e ramp to \u003cb\u003eNew Jersey\u003c/b\u003e",
"polyline" : {
"points" : "w}vwFvdsbMJb@@D?D?D@D?DAB?D?DADADADYz@AFCBA@ABCBC@C@C?A?C?C?C?CACCECMUo@eAa@m@EIEECGEEGEEECEII"
},
"start_location" : {
"lat" : 40.7550018,
"lng" : -73.9951569
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "112 ft",
"value" : 34
},
"duration" : {
"text" : "1 min",
"value" : 6
},
"end_location" : {
"lat" : 40.7563822,
"lng" : -73.9949785
},
"html_instructions" : "Merge onto \u003cb\u003eNY-495 W\u003c/b\u003e",
"maneuver" : "merge",
"polyline" : {
"points" : "qdwwFbdsbMECECCAEAEAE?EAE?E?E?"
},
"start_location" : {
"lat" : 40.7560886,
"lng" : -73.9950563
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.9 mi",
"value" : 1453
},
"duration" : {
"text" : "2 mins",
"value" : 124
},
"end_location" : {
"lat" : 40.7631296,
"lng" : -74.00948579999999
},
"html_instructions" : "Keep \u003cb\u003eright\u003c/b\u003e at the fork to stay on \u003cb\u003eNY-495 W\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eEntering New Jersey\u003c/div\u003e",
"maneuver" : "fork-right",
"polyline" : {
"points" : "kfwwFrcsbMG?E?C@E@E@E@E@EBGBWNEBGDCBMLEDIJ?@IJADEFe@|@?BkBtDCBQ^ABABEFe@bAIRMb@KXGPWx@Wx@Oj@Ib@G\\Kl@CVALCJCL{BzGoA~DoA|DK`@ABUt@[~@CH_@hAcMz`@"
},
"start_location" : {
"lat" : 40.7563822,
"lng" : -73.9949785
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "2.8 mi",
"value" : 4516
},
"duration" : {
"text" : "5 mins",
"value" : 301
},
"end_location" : {
"lat" : 40.7767179,
"lng" : -74.042813
},
"html_instructions" : "Continue onto \u003cb\u003eNJ-495 W\u003c/b\u003e",
"polyline" : {
"points" : "qpxwFh~ubMsJpZsJpZeAbD[rAOt@Ij@E\\?@Cf@A\\?v@@j@@D?BB^D\\BJBL?B@FFVBFBHH\\P`@Tf@Z^X^jAx@HJFLFHJJp@h@`@Zf@`@VPZNb@P^Hl@TVBHDJFJDNJJFJJNJLJPLHHDDHDDBFDHBFBH@H?F?J?HAJAJCHCJEJEJGJGJIHIJKFMHMDKFODODOBQ@O@Q@S?QAO?QCOCQCOEQGOGMIOGMIIGIIGKIIGSISKm@Wg@Qg@Sk@Qi@Qi@Qi@Og@Me@Ke@Mo@Ki@Kg@Ge@Ie@G]C[CM?S?S@QBSDODODMFMDMHML_@^KHINIJGLIPITUl@cIxUMV?BSj@_@lAi@hBEH?@s@bCg@dBQp@?BKXSp@Yt@Wt@_@bAsBhFSf@CDiBzEi@rAYr@eArCSh@Qf@M^Od@Qj@_ErOIXGXIXIXGZK^K`@YbAI\\Sz@m@bCo@`CQp@Mh@Of@Sz@M`@Sx@g@dBIX"
},
"start_location" : {
"lat" : 40.7631296,
"lng" : -74.00948579999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.3 mi",
"value" : 435
},
"duration" : {
"text" : "1 min",
"value" : 21
},
"end_location" : {
"lat" : 40.7797406,
"lng" : -74.04597939999999
},
"html_instructions" : "Keep \u003cb\u003eright\u003c/b\u003e at the fork to continue on \u003cb\u003eNJ-3 W\u003c/b\u003e, follow signs for \u003cb\u003eNew Jersey 3 W\u003c/b\u003e/\u003cb\u003eGarden State Parkway\u003c/b\u003e/\u003cb\u003eSecaucus\u003c/b\u003e",
"maneuver" : "fork-right",
"polyline" : {
"points" : "oe{wFpn|bMc@fAEJIRIPKPKPMPOROPOPONOVIHy@|@oBnBUTURSPc@Ze@Zg@XaAb@"
},
"start_location" : {
"lat" : 40.7767179,
"lng" : -74.042813
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "2.2 mi",
"value" : 3560
},
"duration" : {
"text" : "2 mins",
"value" : 141
},
"end_location" : {
"lat" : 40.8041562,
"lng" : -74.0717843
},
"html_instructions" : "Keep \u003cb\u003eleft\u003c/b\u003e to stay on \u003cb\u003eNJ-3 W\u003c/b\u003e, follow signs for \u003cb\u003eNew Jersey 3 W\u003c/b\u003e/\u003cb\u003eClifton\u003c/b\u003e",
"maneuver" : "keep-left",
"polyline" : {
"points" : "kx{wFjb}bMMJGDEBEBIDeA`@m@R{@VyA`@aB\\qAXiB`@uCl@A?A?QDs@NSFuAXwBf@{@Za@Nm@T_@NYLi@VmAl@gAp@m@`@}@l@QPA@IDA@[XA@_@Ze@`@ONA@i@h@STu@v@eApA}@pAq@bAwAzBgC~DaBhCs@lAq@|@YZo@hA{@pA_A~AsArB{@rAuAvBq@jAUZABA@qFjIe@t@mDpFOVcCvDgElGQVkC|D?@yBhDcDbFy@lAsCfEyGzJ{BdDUZsAzB"
},
"start_location" : {
"lat" : 40.7797406,
"lng" : -74.04597939999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.2 mi",
"value" : 249
},
"duration" : {
"text" : "1 min",
"value" : 13
},
"end_location" : {
"lat" : 40.8062905,
"lng" : -74.07245929999999
},
"html_instructions" : "Take the \u003cb\u003eNJ-120 N\u003c/b\u003e exit toward \u003cb\u003eE Rutherford\u003c/b\u003e",
"maneuver" : "ramp-right",
"polyline" : {
"points" : "_q`xFrcbcMSBA?CBIFOLi@^SJSJOFQFSDODUD]Ba@@g@@_BF"
},
"start_location" : {
"lat" : 40.8041562,
"lng" : -74.0717843
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "410 ft",
"value" : 125
},
"duration" : {
"text" : "1 min",
"value" : 6
},
"end_location" : {
"lat" : 40.807415,
"lng" : -74.07250909999999
},
"html_instructions" : "Keep \u003cb\u003eleft\u003c/b\u003e, follow signs for \u003cb\u003eState Route 503 N\u003c/b\u003e/\u003cb\u003eNew Jersey 120 N\u003c/b\u003e/\u003cb\u003eWashington Avenue\u003c/b\u003e/\u003cb\u003eMoonachie\u003c/b\u003e/\u003cb\u003ePaterson Plank Road\u003c/b\u003e",
"maneuver" : "keep-left",
"polyline" : {
"points" : "i~`xFzgbcMaFH"
},
"start_location" : {
"lat" : 40.8062905,
"lng" : -74.07245929999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.5 mi",
"value" : 869
},
"duration" : {
"text" : "1 min",
"value" : 38
},
"end_location" : {
"lat" : 40.8145647,
"lng" : -74.06878929999999
},
"html_instructions" : "Continue onto \u003cb\u003eNJ-120 N\u003c/b\u003e",
"polyline" : {
"points" : "keaxFdhbcMc@@o@@i@Au@Cg@GaAMuA_@kBk@s@UOGeA_@SIu@[i@Uc@SyAs@o@_@eBeAs@e@i@a@}C_CqBeBA?OM?AAA_@["
},
"start_location" : {
"lat" : 40.807415,
"lng" : -74.07250909999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.2 mi",
"value" : 339
},
"duration" : {
"text" : "1 min",
"value" : 16
},
"end_location" : {
"lat" : 40.8170874,
"lng" : -74.0665298
},
"html_instructions" : "Continue straight onto \u003cb\u003eWashington Ave\u003c/b\u003e",
"maneuver" : "straight",
"polyline" : {
"points" : "_rbxF|pacMm@c@}FcFi@g@SQkB_BA??AA?KK?AAAA?IIEE"
},
"start_location" : {
"lat" : 40.8145647,
"lng" : -74.06878929999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.2 mi",
"value" : 358
},
"duration" : {
"text" : "1 min",
"value" : 38
},
"end_location" : {
"lat" : 40.8143541,
"lng" : -74.0648063
},
"html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003ePaterson Plank Rd\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "yacxFxbacMESAK?G?EBKBQPIFG@ALEPIxDwAp@YhAc@LEr@WFCNG~@[VK"
},
"start_location" : {
"lat" : 40.8170874,
"lng" : -74.0665298
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "79 ft",
"value" : 24
},
"duration" : {
"text" : "1 min",
"value" : 4
},
"end_location" : {
"lat" : 40.8141899,
"lng" : -74.06498189999999
},
"html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e toward \u003cb\u003eN Connection Rd\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "upbxF`x`cMTVHH"
},
"start_location" : {
"lat" : 40.8143541,
"lng" : -74.0648063
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.1 mi",
"value" : 177
},
"duration" : {
"text" : "1 min",
"value" : 25
},
"end_location" : {
"lat" : 40.8138003,
"lng" : -74.06693709999999
},
"html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eN Connection Rd\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "uobxFby`cMFHFFFHPPDzATtDFfA"
},
"start_location" : {
"lat" : 40.8141899,
"lng" : -74.06498189999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.3 mi",
"value" : 548
},
"duration" : {
"text" : "1 min",
"value" : 73
},
"end_location" : {
"lat" : 40.8149711,
"lng" : -74.0728354
},
"html_instructions" : "Continue straight onto \u003cb\u003eRd D\u003c/b\u003e",
"maneuver" : "straight",
"polyline" : {
"points" : "gmbxFjeacMDjA@X@L?DAH?HAHAFAJCHCHCHCFS^Yf@Yp@_@|@ABO\\CHw@lBQb@Q`@AH?BAB?DGRIRGTGZADCJCRCTA^?V@VBVBZF`@R~@Df@Bd@Bn@?^"
},
"start_location" : {
"lat" : 40.8138003,
"lng" : -74.06693709999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "174 ft",
"value" : 53
},
"duration" : {
"text" : "1 min",
"value" : 28
},
"end_location" : {
"lat" : 40.814505,
"lng" : -74.07272910000002
},
"html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eMetLife Stadium Dr\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eDestination will be on the right\u003c/div\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "qtbxFfjbcMf@AHAFA^MBA"
},
"start_location" : {
"lat" : 40.8149711,
"lng" : -74.0728354
},
"travel_mode" : "DRIVING"
}
],
"via_waypoint" : []
}
],
"overview_polyline" : {
"points" : "_rtwFpgubMtBuGVNFPfAr@TNIVkB|FsBpG_C_BqA}@mGeEwH_FsDcCaGwDkNiJsJmG_SoMyByAzA}EPi@nA{Dp@qBtDdCLh@@VCT_@nAKLMBOAIG}@{Aq@eAWWOMIEWEY?UDYJi@\\]^w@|AcC|Ew@bBy@hCg@dBQ`AU~A_ChH_D|J_AzCc@rAwXl|@yLt_@k@hCSrBAtABp@LlALp@FPZ~@Tf@Z^X^jAx@PXRTrCxB~@`@lA^`@HVLv@j@n@h@`@Rb@Dl@Gl@WVQTU^w@PaA@gAKcAMa@Q]c@i@}@g@iDoAeDaAeDq@qC]i@Cg@@eAT[L[Vk@h@SZQ^_@bAeJ`XoAbEmB~GqAvDkDzIsFvNcAvCcFrRkBnHcE`Pq@~Bi@rAk@hA}@hA_@f@cAfAoDjDiAv@iB|@a@XoAf@iBj@yA`@aB\\{Dz@yCl@yA\\mE`A}Aj@qCjAuC~AkBnA_@ZcBxAoApA{BhCoBtCuJrOkAxAkBzCeH~KkAlBeMrR{I|M}CtEyBjD}EpHmLbQ{BdDiBvCUBMJy@l@g@VeAZs@HiABuKT_BEiBUaEkAcA]oCeAmAi@iCsAyCkBgEaDcCuBa@]kHgGkDyCQQOOG_@Fk@h@YfI_DxAi@vAg@^`@h@l@h@dL@v@Ef@Od@m@fAy@nBmAxCc@dAALAHQf@Qv@G^Et@@n@Fr@Z`BHlABnAp@Cj@Q"
},
"summary" : "NJ-495 W and NJ-3 W",
"warnings" : [],
"waypoint_order" : []
}
],
"status" : "OK"
}
答案 0 :(得分:0)
在您的示例中,JSON routes
和legs
实际上是对象数组。因此,要访问内部的任何内容,首先必须获取一个数组元素,然后才能通过其名称访问该对象的属性。
JSONArray legs = parent_array_routes.getJSONObject(0).getJSONArray("legs");
JSONArray steps = legs.getJSONObject(0).getJSONArray("steps");
答案 1 :(得分:0)
String final_json = buffer.toString();
JSONObject parent_object = new JSONObject(final_json);
JSONArray parent_array_routes = parent_object.getJSONArray("routes");
JSONArray legs = parent_array_routes.getJSONObject(0).getJSONArray("legs");
JSONObject temp= legs.getJSONObject(0);
JSONArray steps =temp.getJSONArray("steps");
StringBuffer html_instructions = new StringBuffer();
for(int i = 0; i<steps.length();i++){
JSONObject htm = steps.getJSONObject(i);
html_instructions.append(htm.getString("html_instructions") + "\n");
}
应该这样工作..