에러
java.base/java.lang.String cannot be cast to java.base/java.lang.Integer
JSONObject jsonObject = (JSONObject) obj;
// json Array adStatistics parsing
JSONArray arr = (JSONArray) jsonObject.get("adStatistics");
for (int i = 0; i < arr.size(); i++) {
JSONObject tmp = (JSONObject) arr.get(i);// 인덱스 번호로 접근해서 가져온다.
String coldate = (String) tmp.get("coldate");
String colhour = (String) tmp.get("colhour");
int req_cnt = (int) tmp.get("req_cnt");
int res_cnt = (int) tmp.get("res_cnt");
int ctr_cnt = (int) tmp.get("ctr_cnt");
마지막줄 int req_cnt = (int) tmp.get("req_cnt"); 에서 에러가 발생했다.
원인
JSONObject는 int 로 바로 형변환이 안 되는 것 같다.
해결
int req_cnt = Integer.parseInt((String) tmp.get("req_cnt"));
위와 같은 방식으로 해결해주었다.
후기
첫번째 방식으로 안 되는 정확한 원인을 찾지 못하고 넘어간다.. 아시는 분 있으면 답글 부탁드립니다.
'JAVA > JAVA' 카테고리의 다른 글
[JAVA] 리눅스 자바 설치 위치 찾는 방법 (how to find real installed java path) (0) | 2023.02.05 |
---|---|
[Error] Keycloak The method getLocation() is undefined for the type Response (Eureka com.sun.jersey - jsr311-api 관련) (0) | 2023.01.27 |
[Error] Unrecognized field "xml 필드명" (0) | 2023.01.18 |
[JAVA] JsonIgnore, JsonProperty(READ_ONLY) 차이 (0) | 2022.12.26 |
[JAVA][면접 질문] StringBuilder, StringBuffer 의 차이 (0) | 2022.04.14 |