final String json = "[{\"id\":\"키즈베어\",\"tel\":\"010-1111-2222\"}," +
"{\"id\":\"김꽃드래\",\"tel\":\"010-3333-4444\"}," +
"{\"id\":\"민식이냐\",\"tel\":\"010-5555-6666\"}]" ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.btn);
result = (TextView)findViewById(R.id.result);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
jsonParser();
}
});
}
private void jsonParser(){
String resultStr = "";
try {
//JSON String으로 부터 JSONArray 생성. [](대괄호)
JSONArray jArr = new JSONArray(json);
for (int i = 0; i < jArr.length(); i++) {
//JSONArray에서 i번째 해당하는 JSONObject를 추출.
JSONObject jObj = jArr.getJSONObject(i);
//각 이름("id"/"tel")에 해당하는 값을 추출.
resultStr += String.format("아이디 : %s 전화번호 : %s\n",
jObj.getString("id"), jObj.getString("tel"));
}
result.setText(resultStr);
} catch (JSONException e) {
Toast.makeText(JSONActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
"{\"id\":\"김꽃드래\",\"tel\":\"010-3333-4444\"}," +
"{\"id\":\"민식이냐\",\"tel\":\"010-5555-6666\"}]" ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.btn);
result = (TextView)findViewById(R.id.result);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
jsonParser();
}
});
}
private void jsonParser(){
String resultStr = "";
try {
//JSON String으로 부터 JSONArray 생성. [](대괄호)
JSONArray jArr = new JSONArray(json);
for (int i = 0; i < jArr.length(); i++) {
//JSONArray에서 i번째 해당하는 JSONObject를 추출.
JSONObject jObj = jArr.getJSONObject(i);
//각 이름("id"/"tel")에 해당하는 값을 추출.
resultStr += String.format("아이디 : %s 전화번호 : %s\n",
jObj.getString("id"), jObj.getString("tel"));
}
result.setText(resultStr);
} catch (JSONException e) {
Toast.makeText(JSONActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
[출처] [안드로이드 개발 강좌] JSON 파서|작성자 키즈베어
게시글 목록
| 번호 | 제목 |
|---|---|
| 11040 |
JavaScript
자바스크립트를 이용해 모니터 해상도 구하기
2
|
| 11037 |
JavaScript
모바일 접속시 모바일 페이지로 이동하기
2
|
| 11028 |
MySQL
SQL 문장의 실행순서
8
|
| 11019 |
MySQL
초보자들을 위한 팁(count편)
8
|
| 11017 | |
| 11012 |
MySQL
트리거를 이용한 날짜 자동 업데이트
4
|
| 10999 |
PHP
배열 순서 섞기
12
|
| 10990 |
JavaScript
체크박스 전체선택/해제/반전
8
|
| 10984 |
JavaScript
마우스휠이벤트 등록 및 활용
5
|
| 20478 | |
| 20151 | |
| 10973 |
JavaScript
부분 프린터하기
10
|
| 10970 |
JavaScript
GET으로 넘어온 파라미터 가져오기
2
|
| 10966 |
JavaScript
첨부파일 확장자 체크 [정규식]
3
|
| 10962 |
JavaScript
iframe 아이프레임 투명처리
3
|
| 10961 | |
| 10959 |
PHP
주민번호 검사기
1
|
| 10958 | |
| 10951 |
JavaScript
화면 줌(ZOOM); 필요한분이 있으려나...;
6
|
| 10947 | |
| 10945 | |
| 10944 |
PHP
utf8 체크하기
|
| 20463 | |
| 10943 | |
| 10939 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기