最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript check json output is empty - Stack Overflow

programmeradmin2浏览0评论

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.

This code :

var data = { Name: "John Doe", Age: 25, Address: null, CityState: "Denver, CO" };
for (member in data) {
if (data[member] != null)
    //Do something
}

or

if (myObject == '') {
   alert('this object is empty');
}

Maybe someone can help me find a way out of this example. Please help

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.

This code :

var data = { Name: "John Doe", Age: 25, Address: null, CityState: "Denver, CO" };
for (member in data) {
if (data[member] != null)
    //Do something
}

or

if (myObject == '') {
   alert('this object is empty');
}

Maybe someone can help me find a way out of this example. Please help

Share Improve this question asked Oct 17, 2012 at 10:45 Bertho JorisBertho Joris 1,6015 gold badges28 silver badges60 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

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

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

You want to check if data.items.length > 0. Assuming

var data = {"items":[]};
for (member in data) {
if (data[member] != null)
    //Do something
}

code inside for will not run because length of data is 0

if (myObject == '') {
   alert('this object is empty');
}

myObject wont be null because the object actually is there and its an empty array

you should check for myObject.length because its an empty array

发布评论

评论列表(0)

  1. 暂无评论