2017-03-17 2 views
1

문자열 배열 (유권자 목록)이 포함 된 JSON 객체가 있습니다.Android Volley - 값이 JSON 내부의 배열 확인

여기

가 된 JSONObject로 배열을 가져처럼

http://jsonviewer.stack.hu/#http://www.saveme.ie/api/savings/

내 논리가가는 JSON 배열입니다.

JSON의 배열을 목록에 할당하고 값 목록을 확인하십시오.

String singleSaving = ""; 
ArrayList upVoters = new ArrayList(); 

// Loop through all and get single ID 
for (int i = 0; i < response.length(); i++) { 
    JSONObject obj = null; 
    try { 
     obj = response.getJSONObject(i); 
     _id2 = obj.getString("_id"); 

     if (_id2.equals(_id)) { 
      singleSaving = _id2; 

      // Id like to assign the upVoters array to       
      // the upVoters ArrayList here 

     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 
+0

당신은 무엇을 시도? 그리고 당신이 시도한 것과 직면 한 도전은 무엇입니까 –

+0

JSON의 배열을 알아낼 수있는 로컬 배열에 할당하는 직접적인 방법은없는 것처럼 보입니다. getString을 사용하여 하나의 문자열을 쉽게 할당 할 수 있지만 contains()를 사용하여 배열을 가져 오는 방법을 모르는 경우 – ChrisM

+0

jackson 2를 사용하여 json 응답을 직접 JSON으로/JSON에서 변환하도록 시도 했습니까? – HaroldSer

답변

1
당신은 단지 JSONArray upVoters 이상을 반복하고 ArrayList 채워야 할

:

String singleSaving = ""; 
ArrayList upVoters = new ArrayList(); 

// Loop through all and get single ID 
for (int i = 0; i < response.length(); i++) { 
    JSONObject obj = null; 
    try { 
     obj = response.getJSONObject(i); 
     _id2 = obj.getString("_id"); 

     if (_id2.equals(_id)) { 
      singleSaving = _id2; 

      // Get upVoters list as JSONArray 
      JSONArray upVotersJsonArray = obj.getJSONArray("upVoters"); 
      for(int j=0; j<upVotersJsonArray.length(); j++) { 
       // Fill ArrayList with data from JSON 
       upVoters.add(upVotersJsonArray.getString(j)); 
      }  
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
}