2014-09-02 120 views
0

我是Android新手,想弄清楚如何從Instagram JSON中獲取縮略圖和標準分辨率照片的圖像url,然後我可以使用畢加索下載圖像並將其放入網格視圖。從JSON獲取圖像URL到數組

這是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" 
     } 
    } 
    ] 
} 
+0

你試過GSON?來自谷歌 – NightSkyCode 2014-09-02 17:50:41

回答

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