Page 613 - 3-2
P. 613
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.tvResult1);
Button b = (Button) findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
doJSONParser();
}
});
} // end onCreate()
void doJSONParser(){
StringBuffer sb = new StringBuffer();
String str =
"[{'name':' 배트맨','age':43,'address':' 고담'},"+
"{'name':' 슈퍼맨','age':36,'address':' 뉴욕'},"+
"{'name':' 앤트맨','age':25,'address':'LA'}]";
try {
JSONArray jarray = new JSONArray(str); // JSONArray 생성
for(int i=0; i < jarray.length(); i++){
JSONObject jObject = jarray.getJSONObject(i); // JSONObject 추출
String address = jObject.getString("address");
String name = jObject.getString("name");
int age = jObject.getInt("age");
sb.append(
" 주소:" + address +
" 이름:" + name +
" 나이:" + age + "\n"
);
}
tv.setText(sb.toString());
} catch (JSONException e) {
e.printStackTrace();
}
} // end doJSONParser()
} // end class
- 613 -