В настоящее время я пишу небольшое приложение для Twitter с использованием Twit API.Чтобы сделать то, что мне нужно, я бы хотел, чтобы данные можно было фильтровать по идентификатору пользователя, а не получать весь остальной мусор, который выплевывает 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);
});
последний блок кода закомментирован, потому что я пытался определить «твиты» как data.id, data.statuses.id и т. Д., но все, кажется, выплевывает "неопределенный".Я полный новичок в javascript и JSON, так как я только сейчас изучаю C ++ @ school, поэтому любая помощь будет принята с благодарностью!
edit
Я подумал, что добавлю в сообщение об ошибке, чтобы показать вам, что происходит, когда я пытаюсь обрабатывать данные как объект.
Если я попытаюсь использовать JSON.parse (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