Java Twitter4j获取推文的JSON格式
我正在检索推文并将其存储在文本文件中。如何使用状态对象的JSON格式检索推文,而不是以纯文本字符串格式检索推文。因为一旦检索完成,我将上传到Oracle数据库,这将需要将JSON格式更改为.csv文件。这在纯文本中是不可能的。 我的代码在下面,如有任何帮助,将不胜感激Java Twitter4j获取推文的JSON格式,java,json,twitter4j,Java,Json,Twitter4j,我正在检索推文并将其存储在文本文件中。如何使用状态对象的JSON格式检索推文,而不是以纯文本字符串格式检索推文。因为一旦检索完成,我将上传到Oracle数据库,这将需要将JSON格式更改为.csv文件。这在纯文本中是不可能的。 我的代码在下面,如有任何帮助,将不胜感激 p public static void main(String[] args) { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebu
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);
}
}
此处按原样写入状态..:
PrintWriter pw = new PrintWriter(new FileWriter("TwitterTweetData.txt", true));
pw.println(status);
pw.close();
您的意思是替换pw.println(状态)代码>按
pw.println(DataObjectFactory.getRawJSON(status));
上面的“phiX”回答将把整个Tweet作为Json提供给您。如果您只希望tweet的某些特定部分作为json,请查看。下载项目,根据需要修改com.tweetDownload.dataformat.Tweet和com.tweetDownload.service.CustomStatusListener文件
它基本上是基于搜索词(包括hashtag和提及)和twitter标识的语言下载tweet,基于数据格式类解析相关信息,将该信息转换为Json格式并存储在本地文件系统中。所有这些应用程序参数,包括Twitter OAuth Cred,都是从项目属性文件中读取的。问题是,当我实现整个Tweet的I get The JSON时,我只需要status.getId()的JSON;status.getUser().getScreenName();status.getText();status.getCreatedAt();可能吗?PrintWriter pw=新的PrintWriter(新的FileWriter(“TwitterTweetData.txt”,true));println(DataObjectFactory.getRawJSON(status));关闭();