嗨,这是我在这里发表的第一篇文章。我正在创建一个应用程序来搜索并获取我的活动的#tag推文。 我可以从用户“1.1 / statuses / user_timeline.json?screen_name”成功获取推文 但是当我从“/1.1/search/tweets.json?q=”获取数据时,它不会传递给JSONArray并且我得到一个例外。我相信它的格式略有不同。
如何正确解析JSONArray?
还有其他选择吗?
我使用“https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=”获取的数据为我提供了以下格式的JSON数据
[{“created_at”:“Fri Mar 21 01:07:41 +0000 2014”,“id”:446815376790130689,“id_str”:“446815376790130689”,“text”:“@ konishihiroyuki \ u3069 \ u3046 \ u8aad \ u3093 \ u3067 \ u3082 \ u3042 \ u306a \ u305f \ u306e \ u307b \ u3046 \ u304c \ u304a ....
来自“https://api.twitter.com/1.1/search/tweets.json?q=”的Json数据采用以下格式
{ “状态”:[{ “元数据”:{ “result_type的”: “最近”, “iso_language_code”: “德”}, “created_at”:“Fri Jul 25 16:24:06 +0000 2014”,“id”:492706869530488834,“id_str”:“492706869530488834”,“text”:“Bhagwant Mann EXPOSES Kabaddi Player Scam http://t.co / huGzZRvW59“,”source“:”\ u003ca href = \“http://www.facebook.com/twitter \”rel = \“nofollow \”\ u003eFacebook \ u003c / a \ u003e“,”截断“:false “in_reply_to_status_id”:NULL, “in_reply_to_status_id_str”:NULL, “in_reply_to_user_id”:NULL, “in_reply_to_user_id_str”:NULL, “in_reply_to_screen_name”:NULL, “用户”:{ “ID”:235322641 “ID_STR”: “235322641”, “名称”:” ......
protected ArrayList<Tweet> doInBackground(String... param) {
String searchStr = param[0];
ArrayList<Tweet> tweets = new ArrayList<Tweet>();
HttpURLConnection connection = null;
BufferedReader br = null;
try {
URL url = new URL(URL_SEARCH + searchStr);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
// using the access token (JSON format)
String jsonString = autenticarApp();
JSONObject jsonAcesso = new JSONObject(jsonString);
String tokenPortador = jsonAcesso.getString("token_type") + " "
+ jsonAcesso.getString("access_token");
connection.setRequestProperty("Authorization", tokenPortador);
connection.setRequestProperty("Content-Type",
"application/json");
connection.connect();
// retrieving tweets from api
br = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
StringBuilder strData = new StringBuilder();
while ((line = br.readLine()) != null) {
strData.append(line);
}
Log.d("GET response code", String.valueOf(connection.getResponseCode()));
Log.d("JSON response", strData.toString());
JSONArray jsonArray = new JSONArray(strData.toString());
//debugger stops here and goes directly to exception
JSONObject jsonObject;
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = (JSONObject) jsonArray.get(i);
Tweet tweet = new Tweet();
tweet.setName(jsonObject.getJSONObject("user").getString("name"));
//tweet.setUser(jsonObject.getJSONObject("user").getString("screen_name"));
//tweet.setUrlProfileImage(jsonObject.getJSONObject("user").getString("profile_image_url"));
//tweet.setMessage(jsonObject.getString("text"));
//tweet.setDate(jsonObject.getString("created_at"));
tweets.add(i, tweet);
}
} catch (Exception e) {
Log.e("Error GET: ", Log.getStackTraceString(e));
} finally {
if (connection != null) {
connection.disconnect();
}
}
return tweets;
}
答案 0 :(得分:0)
你获得的字符串不是rappresenting数组,而是一个对象(“状态”) 所以你先用它创建一个json对象,然后是一个json数组...... 类似的东西
JSONObject jobjArray = new JSONObject(strData.toString());
JSONArray jarrTweets = (JSONArray)jobjArray.get("statuses");
JSONObject joTweet;
for (int i = 0; i < jarrTweets.length(); i++)
{
joTweet = (JSONObject) jarrTweets.get(i);
Tweet tweet = new Tweet();
tweet.setName(joTweet.getJSONObject("user").getString("name"));
...