jsontwitter4j

How to get a tweet with its full JSON in Twitter4j


I need to retrive a list of tweets, with many informations (easily retrievable from some Tweet.getX() methods) except for the tweet's entire JSON.

I can't figure out how to get the JSON of a tweet belonging from a QueryResult. Anyone can help me?


Solution

  • You can get the JSON of your tweets by setting setJSONStoreEnabled(true); on the ConfigurationBuilder object that you pass to your TwitterFactory constructor.

    Here's a full example:

    public static void main(String[] args) throws TwitterException {
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setJSONStoreEnabled(true);
    
        Twitter twitter = new TwitterFactory(cb.build()).getInstance();
        Query query = new Query("lizardbill");
        QueryResult result = twitter.search(query);
        for (Tweet tweet : result.getTweets()) {
            System.out.println(tweet.getFromUser() + ":" + tweet.getText());
            String json = DataObjectFactory.getRawJSON(tweet);
            System.out.println(json);
        }
    }