2011-09-20 3 views
2

내가 노력자바 JSONArray 및 동적 키

{ 
    "id":[email protected]", 
    "contact":[ 
    { 
    "uid":0, 
    "name":"johnsmith", 
    "email":[ 
     { 
      "home":"[email protected]" 
     }, 
     { 
      "work":"[email protected]" 
     } 
    ], 
    "mobile":[ 
     { 
      "cc":"+60", 
      "mobile":"00000000" 
     }, 
     { 
      "cc":"+60", 
      "mobile":"00000001" 
     } 
    ] 
    } 
    ] 
} 

이메일 동적 키를 사용하여이 형식의 JSON을

Iterator it = contactArray.getJSONObject(i) 
      .getJSONObject("email").keys(); 

하지만 난 오류

org.json.JSONException: JSONObject["email"] is not a JSONObject. 
받고 있어요

하지만이 방법이 효과적이지만 JSONArray에서 키를 가져 오는 방법은 없습니다.

  JSONArray emailArray = contactArray.getJSONObject(i) 
        .getJSONArray("email"); 

동적 키를 처리하는 방법은 무엇입니까? 감사.

답변

1

배열에는 키가 없으며 요소가 있습니다. 이는 자바 콜렉션 유형에 기초한다, 테스트를 위해 루프 구조를 사용

JSONArray emailArray = contactArray.getJSONObject(i).getJSONArray("email"); 

for(Object o: emailArray){ 
    System.out.println(o); 
} 

emailArray 비어 있으면

는 아무것도 그것의 요소가있는 경우이 값은 출력된다 출력 없을 것이다.