chance 2012. 1. 31. 15:39

 

 

JSONObject

String jsonString = "json string";

 

JSON 데이터는 { } 로 묶인 오브젝트와, 오브젝트 하위에 "키":"값", "키":"값" , 와 같이

,(콤마)로 키-밸류로 이루어진 옵션 값으로 구성된다.

 

{

"TopObject":

{

"KEY01":"VALUE01",

"KEY02":"VALUE02"

"SubObject":

{

"S_KEY01":"S_VALUE01",

"S_KEY02":"S_VALUE02"

}

}

}

 

옵션값은 [ ] 를 사용해 배열로 구성될 수도 있다.

 

파싱

JSONObject json = new JSONObject( jsonString );

 

* 값 얻기

String value = json.getString("KEY01");

 

 

* 오브젝트별 파싱해 값 얻기

JSONObject topObject = json.getJSONObject("TopObject" );

String value1 = topObject.optString("KEY01");

String value2 = topObject.optString("KEY02" );

 

JSONObject subObject = topObject.getJSONObject("SubObject");

String s_value1 = subObject.optString("S_KEY01");

 

배열의 경우는 배열 오브젝트를 getJSONObject() 로 가져오고,

하위 항목들을 다시 getJSONObject( index ) 로 얻어오면 된다.