访问JSON twitter节点模块的特定部分

时间:2017-04-08 10:22:05

标签: javascript node.js twitter npm

我已经完成了使用https://www.npmjs.com/package/twitter从指定帐户获取推文的请求,但是,我无法访问返回的JSON对象的"text"部分。

这是从请求返回的JSON的示例部分:

[ { created_at: 'Sat Apr 08 10:16:51 +0000 2017',
    id: 850653659025334300,
    id_str: '850653659025334272',
    **text: 'Who gets your pick on #GrandNational2017 day? \n\nOur Pinstickers\' guide to the 40-horse race\n\n… ',
    truncated: true,
    entities:
     { hashtags: [Object],
       symbols: [],
       user_mentions: [],
       urls: [Object] },
    source: '<a href="http://www.socialflow.com" rel="nofollow">SocialFlow</a>',
    in_reply_to_status_id: null,
    in_reply_to_status_id_str: null,
    in_reply_to_user_id: null,
    in_reply_to_user_id_str: null,
    in_reply_to_screen_name: null,
    user:
     { id: 265902729,
       id_str: '265902729',
       name: 'BBC Sport',
       screen_name: 'BBCSport',
       location: 'MediaCityUK, Salford',
       description: 'Official account. Also from @bbc - @bbcmotd @bbcf1 @bbctms @bbctennis @bbcrugbyunion & @bbcgetinspired',
       url: '',
       entities: [Object],
       protected: false,
       followers_count: 6620652,
       friends_count: 264,
       listed_count: 17733,
       created_at: 'Mon Mar 14 09:44:40 +0000 2011',
       favourites_count: 161,
       utc_offset: -36000,
       time_zone: 'Hawaii',
       geo_enabled: true,
       verified: true,
       statuses_count: 263835,
       lang: 'en',
       contributors_enabled: false,
       is_translator: false,
       is_translation_enabled: true,
       profile_background_color: 'C0DEED',
       profile_background_image_url: 'http://pbs.twimg.com/profile_background_images/240463705/BBCSportTwitter_backing11.jpg',
       profile_background_image_url_https: 'https://pbs.twimg.com/profile_background_images/240463705/BBCSportTwitter_backing11.jpg',
       profile_background_tile: false,
       profile_image_url: 'http://pbs.twimg.com/profile_images/803606866974609412/Ymsnopmj_normal.jpg',
       profile_image_url_https: 'https://pbs.twimg.com/profile_images/803606866974609412/Ymsnopmj_normal.jpg',
       profile_banner_url: 'https://pbs.twimg.com/profile_banners/265902729/1490963959',
       profile_link_color: '0084B4',
       profile_sidebar_border_color: 'C0DEED',
       profile_sidebar_fill_color: 'DDEEF6',
       profile_text_color: '333333',
       profile_use_background_image: true,
       has_extended_profile: false,
       default_profile: false,
       default_profile_image: false,
       following: true,
       follow_request_sent: false,
       notifications: false,
       translator_type: 'none' },
    geo: null,
    coordinates: null,
    place: null,
    contributors: null,
    is_quote_status: false,
    retweet_count: 0,
    favorite_count: 6,
    favorited: false,
    retweeted: false,
    possibly_sensitive: false,
    lang: 'en' } ]

我只想要console.log JSON的text:部分,我用**标记了。这是我的代码:

const Twitter = require('twitter');

const Twit = new Twitter({
    consumer_key: 'XXX',
    consumer_secret: 'XXX',
    access_token_key: 'XXX',
    access_token_secret: 'XXX'
});

Twit.get('statuses/user_timeline', { screen_name: 'BBCSport', count: 1 }, function(error, tweets, response) {
    if (error) {
        console.log(error)
    }
    console.log(tweets);
});

我已经尝试了console.log(tweets.text),我认为它会起作用,但是我收到一个错误,说它是未定义的。

我自己设法解决了这个问题,我没有意识到推文是一个对象数组,因此无法使用.text表示法进行访问,因此以下解决了它(循环数组,并且访问每个元素上的.text元素。在请求中,Count是count: 1个数字。

for (var i = 0; i < count; i++) {
    console.log(tweets[i].text);
}

0 个答案:

没有答案