简体   繁体   中英

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. 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. 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); by

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

The above answer by 'phiX' will give you entire Tweet as Json. If you only want some specific parts of tweet as json, check this out. Download the project, modify com.tweetDownload.dataformat.Tweet and com.tweetDownload.service.CustomStatusListener files as per your needs.

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. All these application parameters, including Twitter OAuth creds, are read from a project properties file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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