0
$(document).ready(function(){

  /* Union Station */
  $.getJSON("http://myttc.ca/Union_station.json?callback=?",
       function(data){
         if (routes.length > 0) {

            $.each(data.stops, function(i,item){
               $("#Union").append("<p>Stop Name: " + item.name + "</p>");
               $.each(item.routes, function(i,item){
                  $.each(item.stop_times, function(i,item){
                     $("#Union").append("<p>Departure Times: " + item.departure_time + "</p>");
                     $("#Union").append("<p>Shape: " + item.shape + "</p>");
                  });
               });
            });
        }
   });

});     

我在这得到一个空屏幕
谁能帮助修复这个jquery以从json中获取数据我只想显示带有出发时间的停止详细信息

4

4 回答 4

2

它应该是:

function(data){
    if (data.stops.length > 0) {

这是ajax的结果:

Object {time: 1367157909, stops: Array[8], name: "Union Station", uri: "union_station"}

编辑:

我想你需要一些额外的逻辑,这里是对象:

stops: Array[8]
0: Object
1: Object
2: Object
3: Object
4: Object
5: Object
6: Object
7: Object
  agency: "Toronto Transit Commission"
  name: "Union Station Subway Platform"
  routes: Array[1]
    0: Object
      name: "Yonge-University-Spadina Subway"
      route_group_id: "1"
      stop_times: Array[6]
        0: Object
          departure_time: "10:07a"
          departure_timestamp: 1367158079

因此,您可以执行以下操作:

for (var i = 0, l = data.stop.length, stop; i < l; i++) {
    stop = data.stop[i];

    // If current stop has stop_times then...
    if (stop.stop_times.length) {
        // do something...
        console.log(stop.stop_times);
    }
}

示例:http: //jsfiddle.net/fBd3s/

于 2013-04-28T14:06:36.693 回答
1

routes可能不是您想要的:

function(data){
    if (routes.length > 0) {

至少我看不到在哪里routes设置。你可能需要

function(data){
    if (data.length > 0) {
于 2013-04-28T14:05:18.360 回答
1

成功callbak中没有routes,所以去掉那个条件

$(document).ready(function(){

    /* Union Station */
    $.getJSON("http://myttc.ca/Union_station.json?callback=?",
              function(data){

                  $.each(data.stops, function(i,item){
                      $("#Union").append("<p>Stop Name: " + item.name + "</p>");
                      $.each(item.routes, function(i,item){
                          $.each(item.stop_times, function(i,item){
                              $("#Union").append("<p>Departure Times: " + item.departure_time + "</p>");
                              $("#Union").append("<p>Shape: " + item.shape + "</p>");
                          });
                      });
                  });
              });

});    

演示:小提琴

于 2013-04-28T14:06:17.753 回答
0

请参阅接受的这个问题。哪个更具体到 Jquery。带有 jquery 的 jsonp

API 文档将为您提供更多信息。http://api.jquery.com/jQuery.getJSON/

于 2013-04-28T14:06:04.410 回答