Skip to content

Instantly share code, notes, and snippets.

@blundell
Created March 30, 2015 18:27
Show Gist options
  • Save blundell/3953bd0d5a6069502658 to your computer and use it in GitHub Desktop.
Save blundell/3953bd0d5a6069502658 to your computer and use it in GitHub Desktop.
Android Json Parsing of a YouTube response
private Vector<ContentValues> parseJson(String jsonStr) {
Vector<ContentValues> cVVector = new Vector<ContentValues>();
try {
JSONObject json = new JSONObject(jsonStr);
JSONObject data = json.getJSONObject("data");
JSONArray items = data.getJSONArray("items");
for (int i = 0; i < items.length(); i++) {
// Get the JSON object representing the day
JSONObject videoDetails = items.getJSONObject(i);
String title = videoDetails.getString("title");
String description = videoDetails.getString("description");
long duration = videoDetails.getLong("duration");
JSONObject thumbObject = videoDetails.getJSONObject("thumbnail");
String thumbVideoUrl = thumbObject.getString("sqDefault");
String highQualityThumbVideoUrl = thumbObject.getString("hqDefault");
JSONObject playerObject = videoDetails.getJSONObject("player");
String videoUrl = playerObject.optString("default");
ContentValues weatherValues = new ContentValues();
weatherValues.put(VideoEntry.COLUMN_TITLE, title);
weatherValues.put(VideoEntry.COLUMN_DESCRIPTION, description);
weatherValues.put(VideoEntry.COLUMN_DURATION, duration);
weatherValues.put(VideoEntry.COLUMN_THUMB_URL, thumbVideoUrl);
weatherValues.put(VideoEntry.COLUMN_HQ_THUMB_URL, highQualityThumbVideoUrl);
weatherValues.put(VideoEntry.COLUMN_VIDEO_URL, videoUrl);
cVVector.add(weatherValues);
}
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
}
return cVVector;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment