json 파싱방법
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년 전
감사합니다
게시판 목록
개발자팁
개발과 관련된 유용한 정보를 공유하세요.
질문은 QA에서 해주시기 바랍니다.
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 5246 | node.js |
swallow
|
2년 전 | 1105 | |
| 5245 | PHP |
swallow
|
2년 전 | 2527 | |
| 5244 | PHP |
swallow
|
2년 전 | 1286 | |
| 5243 | JavaScript |
swallow
|
2년 전 | 685 | |
| 5242 | node.js |
swallow
|
2년 전 | 666 | |
| 5241 | node.js |
swallow
|
2년 전 | 778 | |
| 5240 | MySQL |
swallow
|
2년 전 | 808 | |
| 5239 | MySQL | 2년 전 | 781 | ||
| 5238 | JavaScript | 2년 전 | 983 | ||
| 5237 | 웹서버 |
swallow
|
2년 전 | 3989 | |
| 5236 | PHP |
swallow
|
2년 전 | 899 | |
| 5235 | PHP |
swallow
|
2년 전 | 1378 | |
| 5234 | 기타 | 2년 전 | 760 | ||
| 5233 | jQuery |
swallow
|
2년 전 | 2036 | |
| 5232 | 2년 전 | 618 | |||
| 5231 | 2년 전 | 1173 | |||
| 5230 | 2년 전 | 1076 | |||
| 5229 | PHP |
swallow
|
2년 전 | 1023 | |
| 5228 | MySQL | 2년 전 | 726 | ||
| 5227 | PHP |
swallow
|
2년 전 | 836 | |
| 5226 | PHP |
swallow
|
2년 전 | 1811 | |
| 5225 | PHP | 2년 전 | 576 | ||
| 5224 | node.js |
swallow
|
2년 전 | 4082 | |
| 5223 | PHP |
swallow
|
2년 전 | 1317 | |
| 5222 | 기타 |
techstar
|
2년 전 | 563 | |
| 5221 | PHP | 2년 전 | 652 | ||
| 5220 |
swallow
|
2년 전 | 904 | ||
| 5219 | 2년 전 | 881 | |||
| 5218 | 2년 전 | 523 | |||
| 5217 | 2년 전 | 1001 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기