开发者

Javascript: How to catch an empty Facebook Graph json object? --> { "data" : [ ] }

I am working with Facebook Graph Api. I am getting new pages of data under the json format and when there is no more data I run into the following:

json object:

{
  "data": [
  ]
}

How can I catch this empty json in javascript? I have tried each of the following individually with no success:

if(json.data[0].length == 0){
    alert('empty');
}

if(json.data[0] == ''){
    alert('empty');
}

if (json.data.length == 0) {
    alert('开发者_Python百科empty');
}

if (json["data"].length == 0) {
    alert('empty');
}

Additional details:

This is the code where I want to check the empty data array. Everything else works fine when I receive non-empty json from Facebook.

$.get(url, function(json){
    pages[pagenumber+1]=json.paging.next;

    //Where I want to check for empty json

    $.each(json.data,function(i, data){
        $("#thumbnail"+i).attr("src", data.picture);
});
},'jsonp');

Updated....!The solution for me was to use:

if (json.data[0])


if(json.data.length == 0){
    alert('empty');
}


Like that:

var json = { "data": [] }

if (json.data.length == 0) {
    alert('empty');
}

or that:

var json = { "data": [] }

if (json["data"].length == 0) {
    alert('empty');
}


In your example here, you need to re-order the statements. Not sure if you are running them in this order but you need to do

if(json.data.length == 0){
    alert('empty');
}

before you do

if(json.data[0].length == 0){
    alert('empty');
}

This might be why it isn't working for you


The solution is the following:

if (json.data[0]) {
alert('empty');
}

Its the only way I got it to work and actually catch the empty data structure.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜