Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在node.js中过滤Twit(API)JSON响应?_Javascript_Json_Twitter - Fatal编程技术网

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

Javascript 如何在node.js中过滤Twit(API)JSON响应?,javascript,json,twitter,Javascript,Json,Twitter,我目前正在使用TwitAPI编写一个小型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!"

我目前正在使用TwitAPI编写一个小型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,
你试过了吗

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

从那里,您应该能够像与对象一样与数据进行交互,并专门获取id或您要查找的任何内容


我也是一个noob,所以我没有深入解释为什么会这样,但它帮助解决了我从API中提取数据时遇到的一个问题。

你能举个例子说明你想将响应转换成什么吗?什么是“垃圾”?您显示的数据不是数组,它看起来像一个对象。因此,只有当
i
是像
id\u str
这样的键之一时,才会定义
tweets[i]
。如果您只需要用户id,请使用
tweets.user.id
@CertainPerformance。例如,我希望看到只显示tweets id的响应。我不关心推文或任何其他方面。“垃圾”正是我不想要的东西,我只是把它当作一种语言上的便利。@Mark\M谢谢你的回答!是的,我明白了。可悲的是,即使这样也不起作用。似乎每当我试图将“数据”作为对象处理时,它都会告诉我它是未定义的。“data.user.id”、“data.id”、“data.statuses.id\u str”都会导致抛出错误“无法读取未定义的属性X”,其中未定义的是“数据”。结果表明数据是数组。如果将来有人遇到这个问题,您可以使用数据[i].[datapoint]迭代对象,谢谢您的响应!刚刚尝试过,但再次出现了这个“未定义”错误,这次引用了“object object”:
$node crypt.js机器人正在启动我们gucie bruh undefined:1[object object object]^SyntaxError:JSON中的意外标记o位于JSON.parse()的位置1处。
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);


});
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