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(' 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(' Departure-time: ' + timez.departure_time + ' || Shape: ' + timez.shape + '<br/>');
});
});
}
});
});
});
</script>
</head>
<body>
<div id="stations">
</div>
</body>
</html>
ありがとう。