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 파서|작성자 키즈베어
댓글 2개
12년 전
java엔 jackson 쓰면편한데
takumi22
12년 전
감사합니다
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7930 | 9년 전 | 469 | ||
| 7929 | 9년 전 | 407 | ||
| 7928 | 9년 전 | 481 | ||
| 7927 | 9년 전 | 384 | ||
| 7926 | 9년 전 | 694 | ||
| 7925 | 9년 전 | 417 | ||
| 7924 | 9년 전 | 402 | ||
| 7923 | 9년 전 | 388 | ||
| 7922 | 9년 전 | 419 | ||
| 7921 | 9년 전 | 435 | ||
| 7920 | 9년 전 | 344 | ||
| 7919 | 9년 전 | 355 | ||
| 7918 | 9년 전 | 509 | ||
| 7917 | 9년 전 | 363 | ||
| 7916 | 9년 전 | 451 | ||
| 7915 | 9년 전 | 462 | ||
| 7914 | 9년 전 | 473 | ||
| 7913 | 9년 전 | 650 | ||
| 7912 | 9년 전 | 479 | ||
| 7911 | 9년 전 | 403 | ||
| 7910 | 9년 전 | 458 | ||
| 7909 | 9년 전 | 572 | ||
| 7908 | 9년 전 | 509 | ||
| 7907 | 9년 전 | 440 | ||
| 7906 | 9년 전 | 462 | ||
| 7905 | 9년 전 | 433 | ||
| 7904 | 9년 전 | 417 | ||
| 7903 | 9년 전 | 429 | ||
| 7902 | 9년 전 | 602 | ||
| 7901 |
|
9년 전 | 770 | |
| 7900 | 9년 전 | 666 | ||
| 7899 | 9년 전 | 446 | ||
| 7898 | 9년 전 | 443 | ||
| 7897 | 9년 전 | 403 | ||
| 7896 | 9년 전 | 419 | ||
| 7895 | 9년 전 | 539 | ||
| 7894 | 9년 전 | 442 | ||
| 7893 | 9년 전 | 424 | ||
| 7892 | 9년 전 | 458 | ||
| 7891 | 9년 전 | 816 | ||
| 7890 | 9년 전 | 1234 | ||
| 7889 | 9년 전 | 771 | ||
| 7888 |
limsy1987
|
9년 전 | 580 | |
| 7887 | 9년 전 | 632 | ||
| 7886 | 9년 전 | 516 | ||
| 7885 | 9년 전 | 489 | ||
| 7884 | 9년 전 | 481 | ||
| 7883 | 9년 전 | 490 | ||
| 7882 | 9년 전 | 545 | ||
| 7881 | 9년 전 | 522 | ||
| 7880 | 9년 전 | 633 | ||
| 7879 | 9년 전 | 521 | ||
| 7878 | 9년 전 | 1296 | ||
| 7877 | 9년 전 | 821 | ||
| 7876 | 9년 전 | 564 | ||
| 7875 | 9년 전 | 634 | ||
| 7874 |
|
9년 전 | 833 | |
| 7873 | 9년 전 | 558 | ||
| 7872 | 9년 전 | 728 | ||
| 7871 | 9년 전 | 544 | ||
| 7870 | 9년 전 | 661 | ||
| 7869 | 9년 전 | 478 | ||
| 7868 | 9년 전 | 519 | ||
| 7867 | 10년 전 | 528 | ||
| 7866 | 10년 전 | 582 | ||
| 7865 | 10년 전 | 529 | ||
| 7864 | 10년 전 | 583 | ||
| 7863 | 10년 전 | 575 | ||
| 7862 | 10년 전 | 538 | ||
| 7861 | 10년 전 | 707 | ||
| 7860 | 10년 전 | 689 | ||
| 7859 | 10년 전 | 457 | ||
| 7858 | 10년 전 | 774 | ||
| 7857 | 10년 전 | 1159 | ||
| 7856 | 10년 전 | 583 | ||
| 7855 | 10년 전 | 820 | ||
| 7854 | 10년 전 | 756 | ||
| 7853 | 10년 전 | 656 | ||
| 7852 | 10년 전 | 577 | ||
| 7851 | 10년 전 | 583 | ||
| 7850 | 10년 전 | 651 | ||
| 7849 | 10년 전 | 415 | ||
| 7848 | 10년 전 | 483 | ||
| 7847 | 10년 전 | 730 | ||
| 7846 | 10년 전 | 508 | ||
| 7845 | 10년 전 | 490 | ||
| 7844 | 10년 전 | 452 | ||
| 7843 | 10년 전 | 488 | ||
| 7842 | 10년 전 | 470 | ||
| 7841 | 10년 전 | 437 | ||
| 7840 | 10년 전 | 454 | ||
| 7839 | 10년 전 | 513 | ||
| 7838 | 10년 전 | 561 | ||
| 7837 | 10년 전 | 394 | ||
| 7836 | 10년 전 | 447 | ||
| 7835 | 10년 전 | 530 | ||
| 7834 |
|
10년 전 | 1230 | |
| 7833 | 10년 전 | 496 | ||
| 7832 | 10년 전 | 469 | ||
| 7831 | 10년 전 | 636 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기