2017-01-22 71 views
-3

請問我想從JSON數組thumbnail_images中獲取URL,它位於另一個medium_large數組內,我如何從第二個數組獲取數據。我已經能夠使用此代碼從這個JSON如何從內部json數組中獲取數據android

"attachments": [ 

{ 
    "id": 367, 
    "url": "http://street2view.com/wp-content/uploads/2017/01/mmm.png", 

得到的網址:

JSONArray attachments = post.getJSONArray("attachments"); 
if (null != attachments && attachments.length() > 0) { 
    JSONObject attachment = attachments.getJSONObject(0); 
    if (attachment != null) 
     item.setAttachmentUrl(attachment.getString("url")); 
} 

所以我怎麼能夠從內部陣列獲取數據,如在此代碼,我怎麼能得到URL

"thumbnail_images": { 
    "medium_large": { 
     "url": "http://street2view.com/wp-content/uploads/2017/01/mmm.png", 
     "width": 749, 
     "height": 400 

回答

-1

我懂了!

  JSONObject images = post.getJSONObject("thumbnail_images"); 
      JSONObject mediumLarge = images.getJSONObject("medium_large"); 
      item.setAttachmentUrl(mediumLarge.optString("url")); 

感謝@classicalConditioning的提示。

+3

請確保接受@ classicalConditioning的答案,如果它有幫助。 –

+0

請不要用答案說謝謝 –

3

這是基於你的例子不具有「thumbnail_images」作爲一個數組,而是它只是一個屬性

JSONObject images = post.getJSONObject("thumbnail_images"); 
JSONObject mediumLarge = images.getJSONObject("medium_large"); 
String url = mediumLarge.optString("url");