简体   繁体   English

Android中的JSON解析(Flickr API)

[英]JSON Parsing in Android (Flickr API)

I've been looking extract image urls from the Flickr api for my android application. 我一直在寻找从Flickr api中提取图像网址的应用程序。

More specifically, I need to parse the first owner and first id field for the following JSON 更具体地说,我需要解析以下JSON的第一个owner和第一个id字段

http://pastebin.com/NVKXdELx http://pastebin.com/NVKXdELx

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"

The element identified by "photo" is a JSON Array, you need to use the JSONArray class "photo"标识的元素是一个JSON数组,您需要使用JSONArray

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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM