从JSON Object获取一些信息并订购它

时间:2017-11-25 10:51:34

标签: arrays json node.js oop google-maps-api-3

我有一个非常简单的问题,但我没有编程经验。我确定有一个简单的解决方案我的问题。我想从谷歌方向api收集一些信息,但不是全部。问题是如何保持订单,因为在步骤数组中,信息提供者在步行和过境模式中有所偏离:(

我有一个JSON对象。

                 "steps" : [
                    {
                       "distance" : {
                          "text" : "0,3 km",
                          "value" : 285
                       },
                       "duration" : {
                          "text" : "4 Minuten",
                          "value" : 212
                       },
                       "end_location" : {
                          "lat" : 48.1157184,
                          "lng" : 11.6600491
                       },
                       "html_instructions" : "Auf \u003cb\u003eMönchbergstraße\u003c/b\u003e nach \u003cb\u003eSüdwesten\u003c/b\u003e Richtung \u003cb\u003eHechtstraße\u003c/b\u003e",
                       "polyline" : {
                          "points" : "_audHssdfA^RLHx@b@TLb@V^Rz@d@|@d@~A|@`@RXN"
                       },
                       "start_location" : {
                          "lat" : 48.1180761,
                          "lng" : 11.6615385
                       },
                       "travel_mode" : "WALKING"
                    },
                    {
                       "distance" : {
                          "text" : "13 m",
                          "value" : 13
                       },
                       "duration" : {
                          "text" : "1 Minute",
                          "value" : 10
                       },
                       "end_location" : {
                          "lat" : 48.1157011,
                          "lng" : 11.6602402
                       },
                       "html_instructions" : "\u003cb\u003eLinks\u003c/b\u003e abbiegen auf \u003cb\u003eSt.-Augustinus-Straße\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eDas Ziel befindet sich auf der linken Seite.\u003c/div\u003e",
                       "maneuver" : "turn-left",
                       "polyline" : {
                          "points" : "grtdHijdfAJ]GG"
                       },
                       "start_location" : {
                          "lat" : 48.1157184,
                          "lng" : 11.6600491
                       },
                       "travel_mode" : "WALKING"
                    }
                 ],
                 "travel_mode" : "WALKING"
              },
              {
                 "distance" : {
                    "text" : "2,5 km",
                    "value" : 2506
                 },
                 "duration" : {
                    "text" : "9 Minuten",
                    "value" : 510
                 },
                 "end_location" : {
                    "lat" : 48.1172364,
                    "lng" : 11.63957
                 },
                 "html_instructions" : "Bus in Richtung Michaelibad",
                 "polyline" : {
                    "points" : "crtdHokdfAFFK^mGpSEPITOp@?@Ml@a@|A}AvGk@~Bk@`C{@xDGLKFI?w@e@C?C?GFyA`G]vAQt@uAnHw@lE]pBQrAYbCq@dF{ArMMt@CDk@Vj@WBELu@YOQIPHXNMt@CDs@ZE?I?GGEGYw@O[AIAMBSFSjBz@XNRHz@XhAb@bA^nEzA~EvBhFzBj@\\]xAOI"
                 },
                 "start_location" : {
                    "lat" : 48.1157011,
                    "lng" : 11.6602402
                 },
                 "transit_details" : {
                    "arrival_stop" : {
                       "location" : {
                          "lat" : 48.1172364,
                          "lng" : 11.63957
                       },
                       "name" : "Heinrich-Wieland-Straße"
                    },
                    "arrival_time" : {
                       "text" : "11:35",
                       "time_zone" : "Europe/Berlin",
                       "value" : 1511606100
                    },
                    "departure_stop" : {
                       "location" : {
                          "lat" : 48.1157011,
                          "lng" : 11.6602402
                       },
                       "name" : "Mönchbergstraße"
                    },
                    "departure_time" : {
                       "text" : "11:26",
                       "time_zone" : "Europe/Berlin",
                       "value" : 1511605590
                    },
                    "headsign" : "Michaelibad",
                    "line" : {
                       "agencies" : [
                          {
                             "name" : "MVG München",
                             "phone" : "011 49 800 344226600",
                             "url" : "http://www.mvg.de/"
                          }
                       ],
                       "color" : "#004d40",
                       "name" : "Neuperlach Süd Bf. - Klinikum Neuperlach - Am Hochacker - St.-Veit-Straße - Michaelibad",
                       "short_name" : "195",
                       "text_color" : "#ffffff",
                       "vehicle" : {
                          "icon" : "//maps.gstatic.com/mapfiles/transit/iw2/6/bus2.png",
                          "name" : "Bus",
                          "type" : "BUS"
                       }
                    },
                    "num_stops" : 5
                 },
                 "travel_mode" : "TRANSIT"
              },...

如何遍历对象获取所有必要信息并保持顺序。我试图建立一个2个对象。所以我在walking.key或transit.key中保存订单。好。但是在使用for循环之后如何以正确的顺序从两个对象中按顺序获取所有信息。我的第一步是否有任何意义?还是有更好的方法?

        var walking = {
                distance : [],
                duration : [],
                html_instructions: [],
                key : []
        };

        var transit = {
                distance : [],
                duration : [],
                html_instructions: [],
                busStopName : [],
                departureTime : [],
                vehicle : [],
                number : [],
                route : [],
                headsign :[],
                key: []
        };

        for (key in myResult) {

              if ( (typeof(myResult[key]["travel_mode"])!=="undefined") && myResult[key]["travel_mode"]=== "WALKING") {


                  walking.distance.push(myResult[key]["distance"]["text"]);
                  walking.duration.push(myResult[key]["duration"]["text"]);
                  walking.duration.push(myResult[key]["html_instructions"]);
                  walking.key.push(myResult[key]);
              }     


              if ( (typeof(myResult[key]["travel_mode"])!=="undefined") && myResult[key]["travel_mode"]=== "TRANSIT") {

                  transit.headsign.push(myResult[key]["transit_details"]["headsign"]);
                  transit.busStopName.push(myResult[key]["transit_details"]["departure_stop"]["name"]);
                  transit.departureTime.push(myResult[key]["transit_details"]["departure_time"]["text"]);
                  transit.distance.push(myResult[key]["distance"]["text"]);
                  transit.duration.push(myResult[key]["duration"]["text"]);
                  transit.vehicle.push(myResult[key]["transit_details"]["line"]["vehicle"]["name"]);
                  transit.number.push(myResult[key]["transit_details"]["line"]["short_name"]);
                  transit.route.push(myResult[key]["transit_details"]["line"]["name"]);
                  transit.key.push(myResult[key]);
              }             

            }

0 个答案:

没有答案