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

});     

I am getting an empty screen in this
can anyone help to fix this jquery to fetch data from json I only want to display the stop details with departure times

4 Answers4

2

It should be:

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

Here's the result of the ajax:

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

EDIT:

I guess you need some extra logic then, here's the object:

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

So you could do something like:

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);
    }
}

Example: http://jsfiddle.net/fBd3s/

Adaz
  • 1,627
  • 12
  • 14
  • Actually i dont want to show the stops without departure time – user2329186 Apr 28 '13 at 14:16
  • See my edit, this should give you a rough idea of how to get the correct stops – Adaz Apr 28 '13 at 14:24
  • if posiible can u fix tht for me? – user2329186 Apr 28 '13 at 14:29
  • Check my last edit, it could probably be more elegant (it's using 3 loops just to format that data) but it should work fine. – Adaz Apr 28 '13 at 14:40
  • Ur logic is correct .Thnks for that .But one problem .it need to display the station name .eg:Union Station Streetcar Bay – user2329186 Apr 28 '13 at 20:05
  • One more thing also......it need to display only stops with active subways....and it wont display bus stop information or empty stops. – user2329186 Apr 28 '13 at 20:49
  • I'd suggest you looking into the code rather than me fixing it - you may want to change it at some point and I may not be there to help you! :) If you are really struggling write down exactly what you need to show and I'll try rewriting the code with as many comments as I can. – Adaz Apr 28 '13 at 21:06
  • i got the idea to fix tht....First we need to display the station name ,then display only stop names with departure times, in that results make a filter for the stop names with "subway" on it.....Can u make the coding like that – user2329186 Apr 28 '13 at 21:56
1

routes is probably not what you're looking for in:

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

At least I can't see where routes would be set. You probably need

function(data){
    if (data.length > 0) {
Arjan
  • 9,784
  • 1
  • 31
  • 41
  • Actually i only want to show the stops with departure time.....when i remove the if condition it will display the result with all stops – user2329186 Apr 28 '13 at 14:24
1

There is no routes in the success callbak, so remove that condition

$(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>");
                          });
                      });
                  });
              });

});    

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

Refer the accepted for this question. Which is more specific to Jquery. jsonp with jquery

And API doc will give you lot more information. http://api.jquery.com/jQuery.getJSON/

Community
  • 1
  • 1
Jaydeep Patel
  • 2,394
  • 1
  • 21
  • 27