2014-11-18 4 views
4

파일에 저장 한 트윗이 있습니다. 사용자를 json 객체로 추출하여 다른 파일에 저장하려고합니다. java.lang.IllegalStateException: Apparently jsonStoreEnabled is not set to true.json tweet에서 json 사용자 가져 오기

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

에서 다음과 같은 결과가 어떻게 truejsonStoreEnabled을 설정할 수 있습니까? 아니면 다른 방법이 있습니까? 나는 Twitter4j을 고집하여 jsonUser을 만들 필요가 없습니다. json-simple을 시도했지만 결과 문자열은 Twitter4j로 구문 분석 할 수 없습니다.

답변

0

그것은 org.json 패키지가이 경우에 도움이되었습니다 밝혀 :

public static String extractUser(String rawJSON) { 
    return new org.json.JSONObject(rawJSON).get("user").toString(); 
} 
4

TwitterObjectFactory는 java.lang.IllegalStateException: Apparently jsonStoreEnabled is not set to true.을 얻을 것이다 경우는 false 것을 registeredAtleastOnce라는 변수가 있습니다. 당신은 당신이 그 오류를 원하지 않는 경우에 당신이 그런 식으로 파일

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 

의 첫 번째 행에 대한 예를 들어, 한 번 만들어 트위터 API를 호출해야합니다, TwitterObjectFactory 그래서이 페이지를 참조 할 수 있습니다 해당 오류가 발생하지 않습니다

+0

json 개체는 파일에서 읽히고 Twitter에서 스트리밍되지 않습니다. – mossaab

+0

Twitter4j는 TwitterObjectFactory에 "registeredAtleastOnce"라는 매개 변수를 가지고 있습니다. False이면 "jsonStoreEnabled가 true로 설정되지 않았습니다."라는 메시지가 표시됩니다. 따라서 최소한 한 번만 구성 작성 도구를 사용해야합니다. – FeanDoe

+0

이 매개 변수는 true로 설정됩니다. 트윗은 API에서 검색되고 setJSONStoreEnabled는 true로 설정됩니다. 전 조건은 내 질문에 적용되지 않습니다. – mossaab