2016-10-26 6 views
0

다음 Json이 있습니다. linkJson은 JsonArray에서 JsonArray를 가져옵니다.

나는 image_hall_listimage_place_list 모두 url 값을 얻고 싶습니다.
다음 코드를 시도했지만 결과가 없습니다.

JSONObject JO = new JSONObject(result); 
JSONArray ja = JO.getJSONArray("image_place_list"); //get the array 

for (int i = 0; i < ja.length(); i++) { 
    JSONObject jo = null; 
    try { 
     jo = ja.getJSONObject(i);      
     jsonurl.add(jo.getString("url")); 
    } catch (JSONException e1) { 
     e1.printStackTrace(); 
    } 
} 

답변

2

이 시도 :

JSONObject JO = new JSONObject(result); 
JSONArray ja = JO.getJSONArray("place_list"); //get the array 
for (int i = 0; i < ja.length(); i++) { 
    JSONObject jo = null; 
    try { 
     jo = ja.getJSONObject(i); 
     JSONArray imageHallList = jo.getJSONArray("image_hall_list"); 
     for (int j = 0; j < imageHallList.length(); j++) { 
      JSONObject oneImageHallList = imageHallList.getJSONObject(j); 
      jsonurl.add(oneImageHallList.getString("url")); 
     } 
     JSONArray imagePlaceList = jo.getJSONArray("image_place_list"); 
     for (int j = 0; j < imagePlaceList.length(); j++) { 
      JSONObject oneImagePlaceList = imagePlaceList.getJSONObject(j); 
      jsonurl.add(oneImagePlaceList.getString("url")); 
     } 
    } catch (JSONException e1) { 
     e1.printStackTrace(); 
    } 
} 
0

중첩 된 for-loop를 사용하십시오. 먼저 image_hall_listimage_place_list 앞에있는 항목을 가져온 다음 값이 저장되면 image_hall_listimage_place_list을 통해 JSON 개체를 가져 와서 JSON 개체의 요소를 반복합니다.

0

내가 몇 가지 방법을 추천 할 것입니다.

하나는 주어진 객체에 대한 모든 URL을 추출합니다.

public ArrayList<String> getURLs(JSONObject jo, String key) throws JSONException { 
    List<String> urls = new ArrayList<String>(); 
    JSONArray arr = jo.getJSONArray(key); 
    for (int j = 0; j < arr.length(); j++) { 
     JSONObject innerObj = arr.getJSONObject(j); 
     urls.add(innerObj.getString("url")); 
    } 
    return urls; 
} 

그런 다음 각 키에 대해 두 번 사용할 수 있습니다. result 변수가 해당 링크에서 직접 나온 경우에는 먼저 "place_list"을 기반으로해야합니다.

try { 
    JSONObject jsonResponse = new JSONObject(result); 
    JSONArray ja = jsonResponse.getJSONArray("place_list"); //get the array 

    for (int i = 0; i < ja.length(); i++) { 
     JSONObject jo = ja.getJSONObject(i);      
     jsonurl.addAll(getURLs(jo, "image_hall_list")); 
     jsonurl.addAll(getURLs(jo, "image_place_list")); 
    } 
} catch (JSONException e1) { 
    e1.printStackTrace(); 
}