0

我正在为http://myttc.ca开发这个网站布局。我成功地检索了大部分数据并将其显示在页面上。

但是,当我必须过滤搜索以仅找到只有地铁的站点名称时,我会遇到错误。这是 JSON 页面,http://myttc.ca/finch_station.json。在这里,当我尝试仅访问路线名称为 的站点时Yonge-University-Spadina Subway,我收到一条错误消息Uncaught TypeError: Cannot call method 'equals' of undefined。我也试过简单地把===操作员。也许我必须取消序列化或其他什么,但在通过互联网解决这个问题后,现在完全无能为力了。我是 javaScript 的新手,我想也许我在这里遗漏了一些东西。到目前为止,这是我的代码:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
      $(document).ready(function(){
        // retrieving the data.
        $.getJSON("http://myttc.ca/finch_station.json?callback=?", function(data){
          //Storing the string in variable.   
          var subway="Yonge-University-Spadina Subway";

          console.log(data);
          //Displaying the station name first in a div called "stations" in the body.
          $("#stations").append('Station: ' + data.name + '<br/>');

          //Iterating through the stops array to display each stop name 
          $.each(data.stops, function(i,stopz){

          $("#stations").append('&nbsp;&nbsp;Stop: ' + stopz.name + '<br/>');
          //This is my problem area.Not able to compare the routes.name to the string.   
          if(stopz.routes.name === subway) {
            $.each(stopz.routes, function(i,routez) {
              //Displaying the Departure time and shape of the stops.
              $.each(routez.stop_times, function(i,timez) {
                $("#stations").append('&nbsp;&nbsp;&nbsp;&nbsp;Departure-time: ' + timez.departure_time + ' || Shape: ' + timez.shape + '<br/>');  
              });
            });
            }
          });
         });
       });
     </script>

 </head>
 <body>

 <div id="stations">
 </div>
 </body>
 </html>

谢谢。

4

1 回答 1

1

给定您的停止数据格式的格式:

{"routes":[],"name":"Finch Station GO Hallway","uri":"finch_station_go_hallway","agency":"Toronto Transit Commission"}

您的引用stopz.routes.name无效。我想你的意思是stopz.name。所以你的代码应该是这样的:

            if(stopz.name === subway)
            {
                $.each(stopz.routes, function(i,routez){
                    // ... etc ...
                });
            }
于 2013-04-28T23:20:07.840 回答