gpt4 book ai didi

javascript - 无法检查空 JSON 键 : value pair

转载 作者:行者123 更新时间:2023-11-30 11:43:22 25 4
gpt4 key购买 nike

我已经尝试过网站上发布的几个答案,例如:

How to check if JSON return is empty with jquery

没有人为我工作。

 xhr.onload = function(){
detalles = xhr.responseText;
console.log("log : "+detalles);
....
}

控制台输出:

log : [0]

Firefox 调试器网络选项卡显示 JSON 0:0

我们将不胜感激您提供的任何帮助。

编辑 - 添加更多代码以扩展上下文

xhr.onload = function(){
detalles = xhr.responseText;
console.log(detalles);

if((!$.trim(detalles))||($.isEmptyObject(detalles))||(!detalles[0])){
console.log("ok");
$("#graficohist").html("No info available");
triggerLoader();
}

最佳答案

你可以简单地创建一个函数来测试对象是否为空

var myObj = {"key" : "value"};
checkEmptyJson = function(myObj){
var count = 0;
for(var key in myObj){
count++;
break;
}
if(count == 0){
return false; // object is empty
}else{
return true; // object is not empty
}
}
var result = checkEmptyJson(myObj);
console.log(result);

在您的上下文中,我们可以将其用作

hasDetallesData =  checkEmptyJson(detalles); // true if Detalles has data

关于javascript - 无法检查空 JSON 键 : value pair,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41856623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com