public static ArrayList<Tweet> fromJsonArray(JSONArray jsonArray) { ArrayList<Tweet> tweets = new ArrayList<Tweet>(); for (int i = 0; i < jsonArray.length(); i += 1) { JSONObject jsonObj = null; try { jsonObj = jsonArray.getJSONObject(i); } catch (JSONException je) { je.printStackTrace(); continue; } Tweet tweet = Tweet.fromJson(jsonObj); if (tweet != null) { tweets.add(tweet); } } return tweets; }
public static Tweet fromJson(JSONObject jsonObj) { Tweet tweet = new Tweet(); try { tweet.body = jsonObj.getString("text"); tweet.id = jsonObj.getLong("id"); tweet.createdAt = jsonObj.getString("created_at"); try { tweet.numFavorites = jsonObj.getLong("favorite_count"); } catch (JSONException j) { tweet.numFavorites = 0l; } try { tweet.numReTweets = jsonObj.getLong("retweet_count"); } catch (JSONException j) { tweet.numReTweets = 0l; } tweet.user = User.fromJson(jsonObj.getJSONObject("user")); try { tweet.favorited = jsonObj.getBoolean("favorited"); } catch (JSONException j) { tweet.favorited = false; } try { tweet.retweeted = jsonObj.getBoolean("retweeted"); } catch (JSONException j) { tweet.retweeted = false; } tweet.mediaUrl = ""; try { JSONArray ja = jsonObj.getJSONObject("entities").getJSONArray("media"); for (int i = 0; i < ja.length(); i += 1) { if ("photo".equals(ja.getJSONObject(i).getString("type"))) { tweet.mediaUrl = ja.getJSONObject(i).getString("media_url"); break; } } } catch (JSONException j) { tweet.mediaUrl = ""; } } catch (JSONException je) { je.printStackTrace(); return null; } return tweet; }