【发布时间】:2010-04-07 18:21:19
【问题描述】:
在 JQuery getJSON 调用中,我如何知道返回的 JSON 的长度?
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.");
}
});
}
由于某种原因,这不起作用,但如果我将 rooms.length > 0 替换为 true,则会打印出完整的房间列表。
如果rooms 为空,则返回为{}。
【问题讨论】:
-
返回的 JSON 有效负载是在方括号
[]还是大括号{}内? -
users.length有什么问题? -
我想我用错了。见上文。
-
哦,我以为那是讽刺。它以对象字面量语法返回。
-
@Rosarch: 唷,现在我们已经解决了这些问题,如果返回
null对您不利,请参阅 api.jquery.com/jQuery.isEmptyObject。
标签: javascript jquery ajax json