private void parseItemsByGson(List<GalleryItem> items, String jsonString) { JsonObject jsonBody = new JsonParser().parse(jsonString).getAsJsonObject(); JsonObject photosJsonObject = jsonBody.get("photos").getAsJsonObject(); JsonArray photosJsonArray = photosJsonObject.get("photo").getAsJsonArray(); for (int i = 0; i < photosJsonArray.size(); i++) { JsonObject photoJsonObject = photosJsonArray.get(i).getAsJsonObject(); GalleryItem item = new GalleryItem(); item.setId(photoJsonObject.get("id").getAsString()); item.setCaption(photoJsonObject.get("title").getAsString()); if (!photoJsonObject.has("url_s")) { continue; } item.setUrl(photoJsonObject.get("url_s").getAsString()); item.setOwner(photoJsonObject.get("owner").getAsString()); items.add(item); } }
private void parseItems(List<GalleryItem> items, String jsonString) throws IOException, JSONException { JSONObject jsonBody = new JSONObject(jsonString); JSONObject photosJsonObject = jsonBody.getJSONObject("photos"); JSONArray photosJsonArray = photosJsonObject.getJSONArray("photo"); for (int i = 0; i < photosJsonArray.length(); i++) { JSONObject photoJsonObject = photosJsonArray.getJSONObject(i); GalleryItem item = new GalleryItem(); item.setId(photoJsonObject.getString("id")); item.setCaption(photoJsonObject.getString("title")); if (!photoJsonObject.has("url_s")) { continue; } item.setUrl(photoJsonObject.getString("url_s")); items.add(item); } }