Javascript:如何测试响应JSON数组是否为空 [英] Javascript: How to test if response JSON array is empty
本文介绍了Javascript:如何测试响应JSON数组是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在取回以下JSON:
I'm getting back the following JSON:
{"array":[],"object":null,"bool":false}
我正在测试它,看似详尽无遗,if语句:
And I'm testing it with the following, seemingly exhaustive, if statement:
$.ajax({
type: "GET",
url: "/ajax/rest/siteService/list",
dataType: "json",
success: function (response) {
var siteArray = response.array;
// Handle the case where the user may not belong to any groups
if (siteArray === null || siteArray=== undefined || siteArray=== '' || siteArray.length === 0) {
window.alert('hi');
}
}
});
但警报未触发。 :[
推荐答案
使用 $ .isArray()检查对象是否是数组。然后你可以检查 length
属性的真实性,看它是否为空。
Use $.isArray() to check whether an object is an array. Then you can check the truthness of the length
property to see whether it is empty.
if( !$.isArray(siteArray) || !siteArray.length ) {
//handler either not an array or empty array
}
这篇关于Javascript:如何测试响应JSON数组是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文