2017-03-17 50 views
0

我試圖解析其中包含一個JSON:JSON解析一個JSONObject無鑰匙

"images": [     
    "https://d1wgio6yfhqlw1.cloudfront.net/sysimages/product/resized6/Interior_St_Pauls_Cathedral_132_12992.jpg", 
    "https://d1kioxk2jrdjp.cloudfront.net/resized/486x324/48-st_pauls_ctahedral_millenirm_bridge.jpg", 
    "http://i4.mirror.co.uk/incoming/article8299330.ece/ALTERNATES/s615b/LOND-2016-052-HMQ-St-Pauls-Thanks-Giving-704JPG.jpg" 
      ] 

的問題是,我不知道如何讓「圖像」節點

中的元素串

我的代碼目前的樣子:

if(c.has("images") && !c.isNull("images")){ 
    JSONArray imagenes = jsonObj.getJSONArray("images"); 
    for (int j = 0; j < imagenes.length(); j++) { 
     JSONObject m = imagenes.getJSONObject(i); 


    } 
} 

我怎樣才能獲得陣列中的每個字符串,而無需使用「鑰匙」?
接下來我不知道該如何處理「M」。

回答

2

你想要一個字符串,而不是一個對象。字符串沒有嵌套鍵。

使用getString(i)方法,而不是getJSONObject(i)

+0

謝謝!這將使它! – Asdemuertes

+0

這個知識幫助了我......我認爲在json數組中只有我們可以得到json對象....... :) –

+1

@KiranBennyJoseph閱讀文檔。 https://developer.android.com/reference/org/json/JSONArray.html –

1

images陣列string值包含不json對象。所以你需要得到string而不是jsonObject

if(c.has("images") && !c.isNull("images")){ 
    JSONArray imagenes = jsonObj.getJSONArray("images"); 
    for (int j = 0; j < imagenes.length(); j++) { 
     String imgURL = imagenes.optString(i); 

     System.out.println(imgURL); 
    } 
} 

輸出:

https://d1wgio6yfhqlw1.cloudfront.net/sysimages/product/resized6/Interior_St_Pauls_Cathedral_132_12992.jpg 

https://d1kioxk2jrdjp.cloudfront.net/resized/486x324/48-st_pauls_ctahedral_millenirm_bridge.jpg 

http://i4.mirror.co.uk/incoming/article8299330.ece/ALTERNATES/s615b/LOND-2016-052-HMQ-St-Pauls-Thanks-Giving-704JPG.jpg