JQuery: Get length of JSON reply?

Posted by Rosarch on Stack Overflow See other posts from Stack Overflow or by Rosarch
Published on 2010-04-07T18:21:19Z Indexed on 2010/04/07 18:43 UTC
Read the original article Hit count: 283

Filed under:
|
|
|

In a JQuery getJSON call, how can I tell the length of the JSON that's returned?

function refreshRoomList() {
    $.getJSON('API/list_rooms',
          function (rooms) {
              if (rooms.length > 0) {
                  $("#existing-room-list").empty();
                  $("#join-existing-room").text("Join existing room:"); // this shouldn't be here
                  $.each(rooms, function (index, roomName) {
                      var newChild = sprintf('<li><a href="room?key=%s">%s</a></li>', index, roomName);
                      $("#existing-room-list").append(newChild);
                  });
              }
              else {
                  $("#join-existing-room").text("No rooms found.");
              }
          });
}

For some reason this doesn't work, but if I replace rooms.length > 0 with true, the full list of rooms is printed out.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery