Question

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!

Was it helpful?

Solution

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);
}

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top