0

I'm using twitter4j to extract tweets from twitter. After i use this line i get a json result:

QueryResult result = twitter.search(query);

I want to know how to use opencsv to write this into csv. writeAll() is only for List or result set, I want to know what i can do with my query result. Many thanks!

idursun
  • 6,261
  • 1
  • 37
  • 51

2 Answers2

0

You need to first create a List<String> from the tweets and then only you can write it using CSVWriter:

List<String[]> lines = new ArrayList<>(result.getTweets().size());
for(Status status : result.getTweets()) {
    lines.add(new String[] { status.getText() });
}
// write the lines using CSVWriter
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
0

I don't think you can use writeAll() directly, as you'll need to build a List<String[]> beforehand.

Alternatively you could use CSVWriter#writeNext(String[]) to write individual Tweets whilst iterating through QueryResult#getTweets(), for example:

for (final Status status : result.getTweets())
{
    final String[] line = new String[] { Long.toString(status.getId()), status.getText(), ... };
    writer.writeLine(line);
}
Jonathan
  • 20,053
  • 6
  • 63
  • 70