简体   繁体   English

Twitter4j获取推文的JSON格式

[英]Twitter4j get JSON format for tweets

I'm retrieving tweets and storing them in a text file. 我正在检索推文并将其存储在文本文件中。 instead of retrieving tweets in a plain text string format, how do I retrieve the tweets with the JSON format for the status objects. 而不是以纯文本字符串格式检索推文,而是如何为状态对象检索JSON格式的推文。 As I will be uploading to an Oracle database once the retrieval is completed, which will require changing the format of JSON to .csv file. 检索完成后,我将上传到Oracle数据库,这需要将JSON格式更改为.csv文件。 which is impossible with plain text. 纯文本是不可能的。 My code is below any help would be appreciated. 我的代码低于任何帮助,将不胜感激。

p public static void main(String[] args) {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
        .setOAuthConsumerKey("")
        .setOAuthConsumerSecret("")
        .setOAuthAccessToken("")
        .setOAuthAccessTokenSecret("");

    StatusListener listener = new StatusListener(){

        public void onStatus(Status status) { 
            long iD = status.getId(); 
            String username = status.getUser().getScreenName(); 
            String tweetText = status.getText(); 
            Date createdAt = status.getCreatedAt();  
            System.out.println(iD+" "+username+" "+tweetText+" "+createdAt);
            String text = iD+" "+username+""+tweetText+" "+createdAt; 
            try {

                PrintWriter pw = new PrintWriter(new FileWriter("TwitterTweetData.txt", true));
                pw.println(text);
                pw.close();
              } catch ( IOException e ) {
                 e.printStackTrace();
              }

        }

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}

        public void onException(Exception ex) {
            ex.printStackTrace();
        }
        public void onScrubGeo(long arg0, long arg1) {}

        public void onStallWarning(StallWarning arg0) {}            
    };

    TwitterStreamFactory tf = new TwitterStreamFactory(cb.build());
    TwitterStream twitterStream = tf.getInstance();
    twitterStream.addListener(listener);

    FilterQuery filtre = new FilterQuery();
    String[] keywordsArray = {"#Hello"}; // Removed Hashtags I'll be using 
    filtre.track(keywordsArray);
    twitterStream.filter(filtre);  
}

}

this here writes the status as it is..: 这是在这里写状态..:

  PrintWriter pw = new PrintWriter(new FileWriter("TwitterTweetData.txt", true));
  pw.println(status);
  pw.close();

you mean replacing the pw.println(status); 您的意思是替换pw.println(status); by 通过

 pw.println(DataObjectFactory.getRawJSON(status));

The above answer by 'phiX' will give you entire Tweet as Json. 上面的“ phiX”答案将为您提供Json的整个Tweet。 If you only want some specific parts of tweet as json, check this out. 如果你只是想鸣叫作为JSON的某些特定部位,检查出。 Download the project, modify com.tweetDownload.dataformat.Tweet and com.tweetDownload.service.CustomStatusListener files as per your needs. 下载项目,根据需要修改com.tweetDownload.dataformat.Tweet和com.tweetDownload.service.CustomStatusListener文件。

What this library basically does is that it download tweets based on search term(s), including hashtags and mentions, and language(s) as identified by twitter, parse relevant information based on a data-format class, converts that information into Json format and stores it on the local fileSystem. 库的主要作用是根据搜索词(包括主题标签和提及内容)以及Twitter识别的语言下载推文,根据数据格式类解析相关信息,然后将该信息转换为Json格式并将其存储在本地文件系统上。 All these application parameters, including Twitter OAuth creds, are read from a project properties file. 从项目属性文件中读取所有这些应用程序参数,包括Twitter OAuth凭据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM