5

I have a tweet that I had stored in a file. I want to extract the user as a json object and store it in a different file. The following results in java.lang.IllegalStateException: Apparently jsonStoreEnabled is not set to true.

String jsonStatus; //Content read from a file
Status status = TwitterObjectFactory.createStatus(jsonStatus); //no problem here
String jsonUser = TwitterObjectFactory.getRawJSON(status.getUser()); //Exception

How can I set jsonStoreEnabled to true? Or, is there another way of doing it? I don't have to stick with Twitter4j to create jsonUser. I tried json-simple, but the resulting String is not parsable with Twitter4j.

mossaab
  • 1,812
  • 4
  • 23
  • 44

2 Answers2

5

TwitterObjectFactory has a variable called registeredAtleastOnce that if false you will get java.lang.IllegalStateException: Apparently jsonStoreEnabled is not set to true.. You can see this page TwitterObjectFactory So, if you don't want that error you will have to make once call to the Twitter API, for example for the first line of your file

ConfigurationBuilder cb = new ConfigurationBuilder();
             cb.setDebugEnabled(true)
             .setOAuthConsumerKey(Ckey)
             .setOAuthConsumerSecret(CkeySecret)
             .setOAuthAccessToken(AToken))
             .setOAuthAccessTokenSecret(AtokenSecret)
             .setJSONStoreEnabled(true);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
String jsonStatus; //Content read from a file
Status status = TwitterObjectFactory.createStatus(jsonStatus); //your status
User u2 = t.showUser(status.getUser().getScreenName()); // you use the twitter object once
String jsonUser = TwitterObjectFactory.getRawJSON(status.getUser()); //now you won't get the error
System.out.print(jsonUser); //your happy json

In that way you wouldn't get that error

FeanDoe
  • 1,608
  • 1
  • 18
  • 30
  • The json object is read from a file, not streamed from Twitter. – mossaab Dec 05 '14 at 21:03
  • Twitter4j has a paramater in the TwitterObjectFactory called "registeredAtleastOnce", if it's False you'll get "Apparently jsonStoreEnabled is not set to true.", so you have to use at least once you configuration builder – FeanDoe Dec 05 '14 at 21:29
  • This parameter is set to true when the tweets are retrieved from the API, and setJSONStoreEnabled is set to true. The former condition does not apply to my question. – mossaab Dec 05 '14 at 21:45
0

It turns out that the org.json package was helpful in this case:

public static String extractUser(String rawJSON) {
    return new org.json.JSONObject(rawJSON).get("user").toString();
}
mossaab
  • 1,812
  • 4
  • 23
  • 44