1

可能的重复:
判断一条推文是否是转推?

因此,我一直在研究一个 twitter 提要脚本,该脚本可以引入我的提要。我想知道如何检查特定推文是否被转推,然后如果它被转推,我想使用转推推文中的信息。这是 twitter API 为转推生成的示例关联数组。

Array
(
    [0] => Array
        (
            [created_at] => Sun Jan 06 08:28:39 +0000 2013
            [id] => 287838076443705345
            [id_str] => 287838076443705345
            [text] => RT @Fullscreen: Heads-up: many YT partners are reporting issues claiming their videos. It appears to be a YT bug. We will keep you posted.
            [source] => web
            ... 
            [user] => Array
                (
                    [id] => 212642904
                    [id_str] => 212642904
                    [name] => Elias Ranz-Schleifer
                    [screen_name] => Xxplosions
                    ... 
                )
                ...
            [retweeted_status] => Array
                (
                    [created_at] => Sun Jan 06 02:08:43 +0000 2013
                    [id] => 287742460824805378
                    [id_str] => 287742460824805378
                    [text] => Heads-up: many YT partners are reporting issues claiming their videos. It appears to be a YT bug. We will keep you posted.
                    ... 
                    [user] => Array
                        (
                            [id] => 238110322
                            [id_str] => 238110322
                            [name] => Fullscreen, Inc.
                            [screen_name] => Fullscreen
                            ... 
                        )
                        ...
                )

            [retweet_count] => 44
            ...
        )

)

所以基本上我只想在它存在的情况下[text] => RT @Fullscreen: Heads-up: many...替换它。[text] => Heads-up: many...如果它不存在,则使用从推文中提取信息的正常方法。我想基本上我想要了解的是如何检查是否[retweeted_status]存在然后提取该信息,如果不存在则提取其他信息。我想避免将 RT 放在那里,因为它似乎是可能的,并使用类似的信息id_str, text, screen_name, etc.

我用来获取常规推文拉取信息的代码是:

id = tweets[i]["id_str"];
twitPic = tweets[0]["user"]["profile_image_url_https"];
content = tweets[i]["text"];
tweetTime = tweets[i]["created_at"];

wherei被 for 循环中的索引替换。如果您对我想要了解的内容有任何疑问,请告诉我,我很乐意为您解答。

4

2 回答 2

1

您可以像这样测试密钥是否存在:

// by using the 'in' operator
content = ('retweeted_status' in tweets[i]) ?  tweets[i]["retweeted_status"] : tweets[i]["text"];
// or by using the method 'hasOwnProperty()'
if(tweets[i].hasOwnProperty('retweeted_status')) {
    content = tweets[i]["retweeted_status"];
} else {
    content = tweets[i]["text"];
}

您也可以检查未定义,但这不是测试键是否存在的准确方法(如果键存在但值实际上未定义怎么办?)。

于 2013-01-06T09:43:14.393 回答
0
// save current tweet
var tweet = tweets[i];
var id = tweet.id_str;
var twitPic = tweet.user.profile_image_url_https;
var tweetTime = tweet.created_at;
var isRetweet = Object.prototype.toString.call(tweet.retweeted_status).slice(8, -1) === 'Object';
// try to use text from original tweet
var content = isRetweet ? tweet.retweeted_status.text : tweet.text;
于 2013-01-06T09:38:13.133 回答