从JSON获取图像URL到数组

时间:2014-09-02 17:40:55

标签: android json

我是Android的新手,我正在试图弄清楚如何将Instagram JSON中的缩略图和标准分辨率照片的图像网址添加到数组中,以便我可以使用Picasso下载图像并将它们放入网格视图。

这是JSON。

{
  "pagination":  {
    "next_url": "https://api.instagram.com/v1/users/1373666362/media/recent?access_token=25320296.1fb234f.b797e861c2494059b6584ac9749208fe&count=2&max_id=791341826737262101_1373666362",
    "next_max_id": "791341826737262101_1373666362"
  },
  "meta":  {
    "code": 200
  },
  "data":  [
     {
      "attribution": null,
      "tags":  [
        "kystatefair"
      ],
      "type": "image",
      "location": null,
      "comments":  {
        "count": 0,
        "data":  []
      },
      "filter": "Amaro",
      "created_time": "1408648864",
      "link": "http://instagram.com/p/r-MuYWFU96/",
      "likes":  {
        "count": 5,
        "data":  [
           {
            "username": "tayworthington_",
            "profile_picture": "http://photos-g.ak.instagram.com/hphotos-ak-xaf1/10632550_835588266460870_765781001_a.jpg",
            "id": "24471760",
            "full_name": "τᎯϓιΘર"
          },
           {
            "username": "renee_laurent",
            "profile_picture": "http://images.ak.instagram.com/profiles/profile_407687505_75sq_1397913189.jpg",
            "id": "407687505",
            "full_name": "Renee Laurent"
          },
           {
            "username": "kystatefair",
            "profile_picture": "http://images.ak.instagram.com/profiles/profile_381460857_75sq_1396983015.jpg",
            "id": "381460857",
            "full_name": "kystatefair"
          },
           {
            "username": "jennaharrod1",
            "profile_picture": "http://photos-b.ak.instagram.com/hphotos-ak-xfa1/10665605_1495839117327497_809128971_a.jpg",
            "id": "18591399",
            "full_name": "Jenna Harrod"
          }
        ]
      },
      "images":  {
        "low_resolution":  {
          "url": "http://scontent-a.cdninstagram.com/hphotos-xaf1/t51.2885-15/10598650_352432021574566_306460147_a.jpg",
          "width": 306,
          "height": 306
        },
        "thumbnail":  {
          "url": "http://scontent-a.cdninstagram.com/hphotos-xaf1/t51.2885-15/10598650_352432021574566_306460147_s.jpg",
          "width": 150,
          "height": 150
        },
        "standard_resolution":  {
          "url": "http://scontent-a.cdninstagram.com/hphotos-xaf1/t51.2885-15/10598650_352432021574566_306460147_n.jpg",
          "width": 640,
          "height": 640
        }
      },
      "users_in_photo":  [],
      "caption":  {
        "created_time": "1408648864",
        "text": "Congratulations to The Lindsey Family for winning the Gospel Quartet competition! #kystatefair",
        "from":  {
          "username": "kyfarmbureau",
          "profile_picture": "http://photos-b.ak.instagram.com/hphotos-ak-xpf1/10349740_650479825030913_1755233568_a.jpg",
          "id": "1373666362",
          "full_name": "Kentucky Farm Bureau"
        },
        "id": "792126548887293629"
      },
      "user_has_liked": false,
      "id": "792126548258148218_1373666362",
      "user":  {
        "username": "kyfarmbureau",
        "website": "",
        "profile_picture": "http://photos-b.ak.instagram.com/hphotos-ak-xpf1/10349740_650479825030913_1755233568_a.jpg",
        "full_name": "Kentucky Farm Bureau",
        "bio": "",
        "id": "1373666362"
      }
    },
     {
      "attribution": null,
      "tags":  [
        "kfbmtc"
      ],
      "type": "image",
      "location": null,
      "comments":  {
        "count": 0,
        "data":  []
      },
      "filter": "Normal",
      "created_time": "1408555318",
      "link": "http://instagram.com/p/r7aTLelU4V/",
      "likes":  {
        "count": 4,
        "data":  [
           {
            "username": "corkey_cole",
            "profile_picture": "http://photos-e.ak.instagram.com/hphotos-ak-xaf1/10598220_490230854445140_2139881142_a.jpg",
            "id": "324166968",
            "full_name": "corkey_cole"
          },
           {
            "username": "renee_laurent",
            "profile_picture": "http://images.ak.instagram.com/profiles/profile_407687505_75sq_1397913189.jpg",
            "id": "407687505",
            "full_name": "Renee Laurent"
          },
           {
            "username": "silveradomafia04",
            "profile_picture": "http://photos-e.ak.instagram.com/hphotos-ak-xfa1/914483_1500860143488988_1771984176_a.jpg",
            "id": "1006562558",
            "full_name": "Gideon Bailey"
          },
           {
            "username": "sharelouisville",
            "profile_picture": "http://images.ak.instagram.com/profiles/profile_1302605134_75sq_1399019203.jpg",
            "id": "1302605134",
            "full_name": "Share Louisville"
          }
        ]
      },
      "images":  {
        "low_resolution":  {
          "url": "http://scontent-a.cdninstagram.com/hphotos-xaf1/t51.2885-15/10598436_1456586981280578_133918080_a.jpg",
          "width": 306,
          "height": 306
        },
        "thumbnail":  {
          "url": "http://scontent-a.cdninstagram.com/hphotos-xaf1/t51.2885-15/10598436_1456586981280578_133918080_s.jpg",
          "width": 150,
          "height": 150
        },
        "standard_resolution":  {
          "url": "http://scontent-a.cdninstagram.com/hphotos-xaf1/t51.2885-15/10598436_1456586981280578_133918080_n.jpg",
          "width": 640,
          "height": 640
        }
      },
      "users_in_photo":  [],
      "caption":  {
        "created_time": "1408555318",
        "text": "Media is starting to crowd around for #kfbmtc",
        "from":  {
          "username": "kyfarmbureau",
          "profile_picture": "http://photos-b.ak.instagram.com/hphotos-ak-xpf1/10349740_650479825030913_1755233568_a.jpg",
          "id": "1373666362",
          "full_name": "Kentucky Farm Bureau"
        },
        "id": "791341827391573199"
      },
      "user_has_liked": false,
      "id": "791341826737262101_1373666362",
      "user":  {
        "username": "kyfarmbureau",
        "website": "",
        "profile_picture": "http://photos-b.ak.instagram.com/hphotos-ak-xpf1/10349740_650479825030913_1755233568_a.jpg",
        "full_name": "Kentucky Farm Bureau",
        "bio": "",
        "id": "1373666362"
      }
    }
  ]
}

2 个答案:

答案 0 :(得分:0)

假设JSON字符串在变量inputJSONString中,您可以按如下方式解析它

List<String> thumbnailUrls = new ArrayList<String>();

JSONObject parsedJSON = new JSONObject(inputJSONString);
JSONArray dataArray = myJson.getString("data");

int thumbnailsCount = dataArray.length();

for(int i; i<thumbnailsCount; i++) {

    JSONObject imagesObject = dataArray.getJSONObject(i).getJSONObject("images");

    String url = imagesObject.getJSONObject("thumbnail").getString("url");

    thumbnailUrls.add(url);
}

答案 1 :(得分:0)

您必须先处理JSON。例如:

// follow your JSON heirarchy!
JSONObject jsonObj = new JSONObject(jsonStr);
JSONObject data = jsonObj.getJSONObject("data");
JSONObject images = jsonObj.getJSONObject("images");
JSONObject lowRes = jsonObj.getJSONObject("low_resolution");
String url = lowRes.getString("url");

现在你有了网址,然后调用毕加索......

Picasso.with(context).load(url).into(imageView);