0

http://myttc.caのこの Web サイト レイアウトを開発しています。ほとんどのデータを取得してページに表示することに成功しました。

しかし、地下鉄しかない駅名だけを見つけるために検索をフィルタリングしなければならなかったとき、エラーで行き詰まりました。これが 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 に答える