Web
json 데이터 뽑아오기
deerfrd
2022. 10. 18. 09:48
반응형
1. response json data 형태
{
"status_code": 200,
"msg": "회원 목록 조회 성공",
"member": [
{
"no": 1,
"name": "홍길동",
"age": "21"
},
{
"no": 2,
"name": "김호이",
"age": "32"
},
{
"no": 3,
"name": "임꺽정",
"age": "28"
}
],
"page_info": {
"total_count": 3,
"total_pages": 1
}
}
2. 데이터 추출
... 중략 ...
JSONObject jsonResponse = (JSONObject)new JSONParser().parse(responseEntity.getBody());
// 1. msg 분리
String msg = jsonResponse.get("msg").toString();
// 2. total_count 분리
JSONObject page_info = (JSONObject)jsonResponse.get("page_info");
int intTotalCnt = (int)page_info.get("total_data_count");
// 3. member array 분리
JSONArray jsonArr = (JSONArray)jsonResponse.get("member");
// jsonArr에서 하나씩 JSONObject로 cast해서 사용
if (jsonArr.size() > 0){
for(int i=0; i<jsonArr.size(); i++){
JSONObject jsonObj = (JSONObject)jsonArr.get(i);
System.out.println("멤버명>> "+(String)jsonObj.get("name"));
}
}
반응형