String result = callMicro(); ... System.out.println(result);
->
{"response":{"body":{"totalCount":108, "item":[...."header"...
JsonObject resultObj = JsonParser.parseString(result).getAsJsonObject(); ... System.out.println(resultObj);
->
{"response":{"body":{"totalCount":108, "item":[...."header"...
"key"
를 넣어주면 "value"
를 뱉어낸다.OpenApiController
@GetMapping("micro")
public String micro() throws Exception{
String result = callMicro();
//json 데이터 파싱하기
// JsonElement jsonElem = JsonParser.parseString(result);
JsonObject resultObj = JsonParser.parseString(result).getAsJsonObject();
//3가지 방법
//resultObj.get("response") -> 리턴타입 : JsonElement
// JsonObject responseObj = resultObj.getAsJsonObject("response");
JsonObject responseObj = resultObj.get("response").getAsJsonObject();
System.out.println("responseObj : "+ responseObj);
// JsonObject responseObj = (JsonObject) resultObj.get("response");
//"key"를 넣어주면 "value"를 뱉어낸다.
JsonObject headerObj = responseObj.getAsJsonObject("header");
System.out.println("headerObj : "+ headerObj);
JsonObject bodyObj = responseObj.getAsJsonObject("body");
System.out.println("bodyObj : "+ bodyObj);
//숫자여도 보통 String으로 받는다.
String totalCount = bodyObj.get("totalCount").getAsString();
System.out.println("totalCount : "+ totalCount);
//"items":[{}] -> 배열이므로
// JsonArray items = bodyObj.getAsJsonArray("items");
JsonArray items = (JsonArray) bodyObj.get("items");
System.out.println("items ::: "+ items);
for(int i=0; i<items.size(); ++i) {
JsonObject item = items.get(i).getAsJsonObject();
System.out.println(item);
}
return "data/micro";
}
//resultObj.get("response") -> 리턴타입 : JsonElement JsonObject responseObj = resultObj.getAsJsonObject("response"); JsonObject responseObj = resultObj.get("response").getAsJsonObject(); JsonObject responseObj = (JsonObject) resultObj.get("response");
System.out.println("items ::: "+ items);
List<ItemVo> itemVoList = new ArrayList<ItemVo>();
for(int i=0; i<items.size(); ++i) {
JsonObject item = items.get(i).getAsJsonObject();
System.out.println(item);
String clearVal = item.get("clearVal").getAsString();
String sn = item.get("sn").getAsString();
String districtName = item.get("districtName").getAsString();
String dataDate = item.get("dataDate").getAsString();
String issueVal = item.get("issueVal").getAsString();
ItemVo vo = new ItemVo();
vo.setClearVal(clearVal);
vo.setSn(sn);
vo.setDistrictName(districtName);
vo.setDataDate(dataDate);
vo.setIssueVal(issueVal);
itemVoList.add(vo);
}
for(ItemVo x : itemVoList) {
System.out.println(x);
System.out.println("---------------------------");
}
@Data
//getter setter toString RequiredArgsConstructor??
public class ItemVo {
private String clearVal;
private String sn;
private String districtName;
private String dataDate;
private String issueVal;
}