Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a tweet with its full JSON in Twitter4j

Tags:

json

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?

like image 872
andreaxi Avatar asked Aug 15 '12 12:08

andreaxi


People also ask

Does twitter use JSON?

All Twitter APIs that return Tweets provide that data encoded using JavaScript Object Notation (JSON). JSON is based on key-value pairs, with named attributes and associated values. These attributes, and their state are used to describe objects. At Twitter we serve many objects as JSON, including Tweets and Users.


1 Answers

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);
    }
}
like image 74
Bill the Lizard Avatar answered Sep 20 '22 12:09

Bill the Lizard