如何在Javascript中将JSON对象与字符串进行比较

如何在Javascript中将JSON对象与字符串进行比较,javascript,json,string,object,equals,Javascript,Json,String,Object,Equals,我正在为您开发此网站布局。我成功地检索了大部分数据并将其显示在页面上 但是,当我不得不过滤我的搜索,只找到只有地铁的车站名称时,我就会陷入错误。这是JSON页面。在这里,当我试图只访问路线名称为Yonge University Spadina Subway的站点时,我得到一个错误,即Uncaught TypeError:Cannot call method'equals'of undefined。我还试着简单地把==操作符放进去。也许我必须取消序列化或者做些什么,但是在通过互联网解决这个问题后,

我正在为您开发此网站布局。我成功地检索了大部分数据并将其显示在页面上

但是,当我不得不过滤我的搜索,只找到只有地铁的车站名称时,我就会陷入错误。这是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>

$(文档).ready(函数(){
//检索数据。
$.getJSON(“http://myttc.ca/finch_station.json?callback=?,函数(数据){
//将字符串存储在变量中。
var subway=“永和大学斯帕迪纳地铁”;
控制台日志(数据);
//在正文中名为“stations”的div中首先显示station名称。
$(“#站”).append('Station:'+data.name+'
'); //遍历stops数组以显示每个stop名称 $.each(data.stops,function(i,stopz){ $(“#站”).append('Stop:'+stopz.name+'
'); //这是我的问题区域。无法将routes.name与字符串进行比较。 if(stopz.routes.name==地铁){ $.each(stopz.routes,function(i,routez){ //显示出发时间和车站形状。 $。每个(routez.stop_次,函数(i,timez){ $(“#站”).append('出发时间:'+timez.detairation_时间+'|形状:'+timez.Shape+'
'); }); }); } }); }); });

谢谢。

根据站点数据格式:

{"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 ...
                });
            }

使用浏览器的调试器——Chrome的开发工具、IE的开发工具、Firefox的Firebug插件(都是用F12启动的)。Undefined表示您正在使用未声明的变量。通过快速查看,我发现“name”属性属于“routes”数组;试着把每个美元都换成美元。