JavaScript检查json输出是否为空 [英] Javascript check json output is empty
本文介绍了JavaScript检查json输出是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我在json中有这样的数据:
If I have data in json like this :
{"items":[{"id":"2049","channel_code":"HBD","channel_name":"HBO HD"}]}
如果您将我的数据搜索到服务器,则找不到这样的结果:
And if you search my data to the server could not find results like this :
{"items":[]}
上面的输出,如何读取我的数据不存在或为空?
Of output as above, how do I read that my data does not exist or is empty?
我已经写了一些代码,但是没有找到想要的结果.
I have written some code that I got but have not found the results I want.
此代码:
var data = { Name: "John Doe", Age: 25, Address: null, CityState: "Denver, CO" };
for (member in data) {
if (data[member] != null)
//Do something
}
或
if (myObject == '') {
alert('this object is empty');
}
也许有人可以帮助我找到解决这个问题的方法. 请帮助
Maybe someone can help me find a way out of this example. Please help
推荐答案
To check whether your array is empty, just use the respective length
property:
if ( data['items'].length < 1 ) {
// it's empty
}
这篇关于JavaScript检查json输出是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文