我想抓取twitter的推特與特殊的#hashtag和從返回的json我只需要身份證,日期,用戶ID,在cvs中導出的推文的文本。如何使用Twitter的API JSON與java
我的代碼看起來像這樣。
import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.*;
public class CrawlTweets {
static String AccessToken = "xxx";
static String AccessSecret = "xxx";
static String ConsumerKey = "xxx";
static String ConsumerSecret = "xxx";
/**
* @param args
*/
public static void main(String[] args) throws Exception {
OAuthConsumer consumer = new CommonsHttpOAuthConsumer(
ConsumerKey,
ConsumerSecret);
consumer.setTokenWithSecret(AccessToken, AccessSecret);
HttpGet request = new HttpGet("https://api.twitter.com/1.1/search/tweets.json?q=%23Nutella&lang=en");
consumer.sign(request);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(request);
JSONObject obj = new JSONObject(response);
String id = obj.get("id").toString();
System.out.println(id);
}
}
我得到以下異常:
Exception in thread "main" org.json.JSONException: JSONObject["id"] not found.
at org.json.JSONObject.get(JSONObject.java:459)
at CrawlTweets.main(CrawlTweets.java:41)
什麼是錯的代碼?我如何提取所提及的信息並提交給cvs?謝謝。
不,答案完全正確。我猜obj.get(「id」)是問題...如果我將它改爲obj.get(「statuses」)。toString()它返回的東西太多了。我只想要文字,日期和用戶。我不明白如何訪問這些信息。 – desperateCoder