Reputation: 6845
I've been looking extract image urls from the Flickr api for my android application.
More specifically, I need to parse the first owner
and first id
field for the following JSON
Hence, I tried
JSONObject json = new JSONObject(jsonString);
JSONObject photos = json.getJSONObject("photos");
JSONObject photo = photos.getJSONObject("photo");
String picOwner = photo.getString("owner");
String picID = photo.getString("id");
To me, the logic makes sense (grab the row photos, then grab the row photo, then extract the owner field as a string). However, an error is thrown on
JSONObject photo = photos.getJSONObject("photo");
Am I overlooking something?
Note: The ultimate values I am looking for are
"id": "8637130199",
"owner": "93693022@N07"
Upvotes: 0
Views: 812
Reputation: 280132
The element identified by "photo"
is a JSON Array, you need to use the JSONArray
class
JSONObject json = new JSONObject(jsonString);
JSONObject photos = json.getJSONObject("photos");
JSONArray photo = photos.getJSONArray("photo");
if (photo.length() > 0) {
JSONObject first = photo.getJSONObject(0);
String picOwner = first.getString("owner");
String picID = first.getString("id");
}
Upvotes: 4