2011. 6. 15. 17:36
1. JSONArray
{
2.JSONObject
{
"color":{
3.aptana
트리형식으로 json데이터를 보는데 도와준다./view JSONdata to tree type
http://codedb.tistory.com/entry/Android-plugging-Aptana-in-eclipe-%EC%9D%B4%ED%81%B4%EB%A6%BD%EC%8A%A4%EC%97%90%EC%84%9C-aptana-%EC%82%AC%EC%9A%A9%ED%95%98%EA%B8%B0 <- install aptana in eclipse
{
"number":[
1,
2,
3,
4,
5
]
2,
3,
4,
5
]
}
String json4arr="{\"number\":[1,2,3,4,5]}";
try {
int sum=0;
JSONObject jobj = new JSONObject(json4arr);
JSONArray jarr = jobj.getJSONArray("number");
for(int i=0;i<jarr.length();i++){
int tmp=jarr.getInt(i);
if(tmp%2==1){
sum += tmp;
}
}
} catch (JSONException e) {
e.printStackTrace();
} 2.JSONObject
{
"color":{
"top":"red",
"bottom":"black",
"left":"blue",
"right":"green"
}
"bottom":"black",
"left":"blue",
"right":"green"
}
}
String json4obj="{\"color\":{\"top\":\"red\",\"bottom\":\"black\",\"left\":\"blue\",\"right\":\"green\"}}";
try {
JSONObject jobj = new JSONObject(json4obj);
JSONObject color = jobj.getJSONObject("color");
String top_color="";
if(color.has("left")){
top_color=color.getString("left");
}
} catch (JSONException e) {
e.printStackTrace();
}
3.aptana
트리형식으로 json데이터를 보는데 도와준다./view JSONdata to tree type
http://codedb.tistory.com/entry/Android-plugging-Aptana-in-eclipe-%EC%9D%B4%ED%81%B4%EB%A6%BD%EC%8A%A4%EC%97%90%EC%84%9C-aptana-%EC%82%AC%EC%9A%A9%ED%95%98%EA%B8%B0 <- install aptana in eclipse
안녕하세요 ^^
잠시 설명을 해볼까 해요.
1번째는 json array인데요 [] 이런형식으로 데이터가 배열(array)형식으로 되어있는 데이터를 가져옵니다. 예제 json array 샘플에서 number데이터가 그러한 예이죠.
노란색으로 표현하였듯이 getJSONArray로 number데이터를 가져와 데이터타입에 맞게 index를 넣어 get해오는데요. 예제는 숫자데이터라 getInt를 사용하였는데 그부분은 잘 구미에맞게 자유자재로 사용하실꺼라고 생각해요 ㅎ
2번째는 json object인데요. getJSONObject로 데이터를 get해오죠? 그리고 has라는 부분에 노란색으로 표시하였는데요. has는 말그대로 해당데이터가 있는지 없는지 true/false로 알아내는 함수입니다. 데이터를 가져오려하는데 없다면 곤란하겠죠? 예외처리로 혹은 다른 용도로 has로 해당데이터가 존재하는지 유무를 따질때 유명하겠네요 ㅎ
그럼 도움되셨으면 좋겠네요 ㅎ
enjoy coding! Don't be angry!
'Programming > Android' 카테고리의 다른 글
Android - Download file for HTTP / HTTP로 파일다운받기 (0) | 2011.07.19 |
---|---|
Android - plugging Aptana in eclipe / 이클립스에서 aptana 사용하기 (0) | 2011.06.15 |
Android - set pinch zoom in webview / 웹뷰에서 핀치 줌 설정 (0) | 2011.06.15 |
Android - Resourece path / 리소스 경로 (0) | 2011.06.14 |
Android - read & write ini(properties) in android SDcard/ 안드로이드에서 ini(properties) 사용하기 (0) | 2011.06.14 |