遍历Json并仅返回一些属性

时间:2017-11-18 21:05:21

标签: javascript json node.js object

是否可以搜索对象属性并访问同一级别的所有内容?

我有步骤对象,可以从1到n属性(方向步骤)。我想只选择同一级别的属性值:

"travel_mode" : "TRANSIT"

这意味着我可以访问例如name属性,该属性在travkl_mode行走的级别中不可用

                 "steps" : [
                    {
                       "distance" : {
                          "text" : "0,3 km",
                          "value" : 285
                       },
                       "travel_mode" : "WALKING"
                    },
                    {
                       "distance" : {
                          "text" : "13 m",
                          "value" : 13
                       },
                       "travel_mode" : "WALKING"
                    }
                 ],
                 "travel_mode" : "WALKING"
              },
              {
                 "distance" : {
                    "text" : "2,5 km",
                    "value" : 2506
                 },
                    "departure_stop" : {
                       "location" : {
                          "lat" : 48.1157011,
                          "lng" : 11.6602402
                       },
                       "name" : "Mönchbergstraße"
                 },
                 "travel_mode" : "TRANSIT"
              },
              {
                 "distance" : {
                    "text" : "0,2 km",
                    "value" : 230
                 },
                       "travel_mode" : "WALKING"
                    },
                    {
                       "distance" : {
                          "text" : "15 m",
                          "value" : 15
                       },
                       "duration" : {
                          "text" : "1 Minute",
                          "value" : 16
                       },
                       "end_location" : {
                          "lat" : 48.1175728,
                          "lng" : 11.6371798
                       },
                       "html_instructions" : "\u003cb\u003eLinks\u003c/b\u003e Richtung \u003cb\u003eHeinrich-Wieland-Straße\u003c/b\u003e abbiegen",
                       "maneuver" : "turn-left",
                       "polyline" : {
                          "points" : "u~tdHu{_fAZH"
                       },
                       "start_location" : {
                          "lat" : 48.1177065,
                          "lng" : 11.6372328
                       },
                       "travel_mode" : "WALKING"
                    },
                    {
                       "distance" : {
                          "text" : "35 m",
                          "value" : 35
                       },
                       "duration" : {
                          "text" : "1 Minute",
                          "value" : 24
                       },
                       "end_location" : {
                          "lat" : 48.1176498,
                          "lng" : 11.6367222
                       },
                       "html_instructions" : "\u003cb\u003eRechts\u003c/b\u003e abbiegen auf \u003cb\u003eHeinrich-Wieland-Straße\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eDas Ziel befindet sich auf der linken Seite.\u003c/div\u003e",
                       "maneuver" : "turn-right",
                       "polyline" : {
                          "points" : "y}tdHk{_fAAHK|@AR"
                       },
                       "start_location" : {
                          "lat" : 48.1175728,
                          "lng" : 11.6371798
                       },
                       "travel_mode" : "WALKING"
                    }
                 ],
                 "travel_mode" : "WALKING"
              }
           ],
           "traffic_speed_entry" : [],
           "via_waypoint" : []
        }
     ],

我尝试过类似的东西:

        var answer = JSON.parse(body), busName;

        for(key in answer){                               
            if(key.indexOf("name") != -1){             
                busName = answer[key];                
                break;                                  
            }
        }

但是我在busName中得到了未定义的值

1 个答案:

答案 0 :(得分:1)

我会帮助你验证json。

var answer = {
  "steps": [
    {
    "distance": {
      "text": "0,3 km",
      "value": 285
    },
    "travel_mode": "WALKING"
  },
  {
    "distance": {
      "text": "13 m",
      "value": 13
    },
    "travel_mode": "WALKING"
  },
  {
    "distance": {
      "text": "2,5 km",
      "value": 2506
    },
    "departure_stop": {
      "location": {
        "lat": 48.1157011,
        "lng": 11.6602402
      },
      "name": "Mönchbergstraße"
    },
    "travel_mode": "TRANSIT"
  },
  {
    "distance": {
      "text": "0,2 km",
      "value": 230
    },
    "travel_mode": "WALKING"
  },
  {
    "distance": {
      "text": "15 m",
      "value": 15
    },
    "duration": {
      "text": "1 Minute",
      "value": 16
    },
    "end_location": {
      "lat": 48.1175728,
      "lng": 11.6371798
    },
    "html_instructions": "<b>Links</b> Richtung <b>Heinrich-Wieland-Straße</b> abbiegen",
    "maneuver": "turn-left",
    "polyline": {
      "points": "u~tdHu{_fAZH"
    },
    "start_location": {
      "lat": 48.1177065,
      "lng": 11.6372328
    },
    "travel_mode": "WALKING"
  },
  {
    "distance": {
      "text": "35 m",
      "value": 35
    },
    "duration": {
      "text": "1 Minute",
      "value": 24
    },
    "end_location": {
      "lat": 48.1176498,
      "lng": 11.6367222
    },
    "html_instructions": "<b>Rechts</b> abbiegen auf <b>Heinrich-Wieland-Straße</b><div style=\"font-size:0.9em\">Das Ziel befindet sich auf der linken Seite.</div>",
    "maneuver": "turn-right",
    "polyline": {
      "points": "y}tdHk{_fAAHK|@AR"
    },
    "start_location": {
      "lat": 48.1175728,
      "lng": 11.6371798
    },
    "travel_mode": "WALKING"
  }
 ],
 "traffic_speed_entry": [],
 "via_waypoint": []
};

将json粘贴到online json editor中,看看它是否为您提供了所期望的结构。您将不得不修复生成json的方式,因为它无效。

用我的json:

console.log(answer.steps[2].departure_stop.name); // ​​​​​Mönchbergstraße​​​​​

for (key in answer.steps) {
  if (answer.steps[key].departure_stop) {
    console.log(answer.steps[key].departure_stop.name); // ​​​​​Mönchbergstraße

    // do what you want with the answer.steps[key] object here
    //  answer.steps[key].travel_mode
  }
}

以上假设所有departure_stop对象都有一个“name”键。如果他们不这样做,你也应该在尝试访问它之前对它进行空检查。

for (key in answer.steps) {
  if (answer.steps[key].travel_mode && answer.steps[key].travel_mode === "TRANSIT") {

    // do what you want with the answer.steps[key] object here
    console.log(answer.steps[key].departure_stop.name); // ​​​​​Mönchbergstraße
  }
}

我希望这会有所帮助,目前还不清楚你究竟想要完成什么。