如何在node.js中过滤Twit(API)JSON响应?

时间:2018-06-25 03:27:55

标签: javascript json twitter

我目前正在使用Twit API编写一个小型Twitter应用程序。为了完成我需要做的事情,我希望数据能够按用户ID进行过滤,而不希望将所有其他垃圾JSON垃圾吐出来。响应如下所示:

{ created_at: 'Sat Jun 23 03:45:13 +0000 2018',
  id: 1010368149466697700,
  id_str: '1010368149466697728',
  text:
   'RT @ClassicIsComing: "Let\'s Talk ETC!" Podcast Series by @chris_seberino of @InputOutputHK \nA deep series of powerful intervie
ws with influ…',
  truncated: false,
  entities:
   { hashtags: [],
     symbols: [],
     user_mentions: [ [Object], [Object], [Object] ],
     urls: [] },
  source:
   '<a href="https://about.twitter.com/products/tweetdeck" rel="nofollow">TweetDeck</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: 759252279862104000,
     id_str: '759252279862104064',
     name: 'Ethereum Classic',
     screen_name: 'eth_classic',
     location: 'Blockchain',
     description:
      'Latest News and Information from Ethereum Classic (ETC). A crypto-currency with smart contracts which respects immutability a
nd neutrality.',
     url: ,
     entities: { url: [Object], description: [Object] },
     protected: false,
     followers_count: 216255,
     friends_count: 538,
     listed_count: 2147,

等我正在使用的代码是:

T.get('statuses/home_timeline', {count: 1, exclude_replies: true}, 
function(err, data, response){
    if (err){
        console.log('Uh oh, we got a problem');
    }
    else{
        console.log('We GUUCie bruh');
    }

    var tweets = data;


   /* for (var i = 0; i < tweets.length; i++) { 
             console.log(tweets[i]);
         }          */
    console.log(data);


});

最后一段代码被注释掉了,因为我试图将“ tweets”定义为data.id,data.statuses.id等,但是所有内容似乎都吐出了“ undefined”。我对javascript和JSON完全陌生,因为我目前仅在学习C ++ @ school,因此任何帮助将不胜感激!

edit

我想我会在错误消息中添加内容,以向您展示当我尝试将数据视为对象时会发生什么。

如果我尝试将JSON.parse(data)用作我的tweet变量的值:

T.get('statuses/home_timeline', {count: 1, exclude_replies: true}, callBackFunction)

function callBackFunction(err, data, response){
    if (err){
        console.log('Uh oh, we got a problem');
    }
    else{
        console.log('We GUUCie bruh');
    }


    var tweets = JSON.parse(data);



//for (var i = 0; i < tweets.length; i++) { 
  //           console.log(tweets[i].id_str);
    //     }          
    console.log(tweets.id_str);

}

我得到:

$ node crypt.js
the bot is starting
We GUUCie bruh
undefined:1
[object Object]
 ^
SyntaxError: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)

如果我尝试立即将其视为对象,请使用:

function callBackFunction(err, data, response){
    if (err){
        console.log('Uh oh, we got a problem');
    }
    else{
        console.log('We GUUCie bruh');
    }


    var tweets = data.id_str;



//for (var i = 0; i < tweets.length; i++) { 
  //           console.log(tweets[i].id_str);
    //     }          
    console.log(tweets);

}

我得到:

$ node crypt.js
the bot is starting
We GUUCie bruh
undefined

1 个答案:

答案 0 :(得分:1)

您尝试过JSON.parse吗?

因此,您的行“ var tweets = data;”将是“ var tweets = JSON.parse(data);”

从那里,您应该能够像对待对象一样与数据进行交互,并专门获取ID或所需的内容。

我也是菜鸟,所以我没有深入解释它为什么起作用,但是它有助于解决我从API提取数据时遇到的问题。

相关问题