누

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();
  }
    }
}
|

댓글 2개

java엔 jackson 쓰면편한데
댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

5,403건

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
MySQL 12-12-30 조회 6,866
웹서버 12-12-30 조회 1.9만
Mobile 12-12-29 조회 4,816
PHP 12-12-28 조회 6,039
PHP 12-12-28 조회 5,439
PHP 12-12-28 조회 2,769
jQuery 12-12-27 조회 3,937
jQuery 12-12-27 조회 3,203
JavaScript 12-12-27 조회 3,691
PHP 12-12-27 조회 2,696
PHP 12-12-27 조회 4,112
jQuery 12-12-27 조회 1만
웹서버 12-12-26 조회 5,276
PHP 12-12-20 조회 5,380
JavaScript 12-12-20 조회 1.7만
JavaScript 12-12-20 조회 2,727
PHP 12-12-18 조회 9,289
jQuery 12-12-18 조회 2,878
OS 12-12-15 조회 6,061
OS 12-12-15 조회 7,375
웹서버 12-12-15 조회 8,004
PHP 12-12-14 조회 4,797
OS 12-12-12 조회 4,637
OS 12-12-12 조회 8,572
jQuery 12-12-12 조회 3,640
jQuery 12-12-12 조회 7,638
jQuery 12-12-12 조회 9,494
PHP 12-12-10 조회 2,441
PHP 12-12-10 조회 2,506
OS 12-12-10 조회 6,145