我有这个返回的JSON:

results: Array[15]
0: Object
created_at: "Wed, 10 Aug 2011 22:45:36 +0000"
from_user: "CriisBellaFlor"
from_user_id: 360990380
from_user_id_str: "360990380"
geo: null


我正在使用以下代码对其进行循环:

var twitterUrl='http://search.twitter.com/search.json?callback=?&q=';
                query='cinema';
                $.getJSON(twitterUrl+query,function(json){
                    console.log(json);
                    $.each(json.results,function(i,tweet){
                        if(tweet.iso_language_code == 'en'){
                            if(tweet.geo === 0){
                                $(cinemas).append('<img src="'+tweet.profile_image_url+'" width="48" height="48" />'+tweet.text+'<br />');
                            } else {
                                $(cinemas).append('<img src="'+tweet.profile_image_url+'" width="48" height="48" />'+tweet.text+'Coordinates:' + tweet.geo.coordinates[0] + ' + ' + tweet.geo.coordinates[1] + '<br />');
                            }

                        }
                    });
                });


所以我想确定的是,如果“ geo”为空,请不要尝试显示坐标,如果有坐标,则显示坐标。

但是我得到这个错误:

Uncaught TypeError: Cannot read property 'coordinates' of null

最佳答案

试试看

if(tweet.geo === null){


顺便说一句,===表示比较检查也可以是变量类型

10-07 12:55
查看更多