JavaScript检查json输出是否为空

编程入门 行业动态 更新时间:2024-10-11 03:23:33
本文介绍了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

推荐答案

要检查数组是否为空,只需使用相应的 length属性:

To check whether your array is empty, just use the respective length property:

if ( data['items'].length < 1 ) { // it's empty }

更多推荐

JavaScript检查json输出是否为空

本文发布于:2023-10-28 00:00:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534912.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:为空   JavaScript   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!