지난 공공데이터를 작업하면서 Json으로 return 받은 데이터를 객체화하기위한 구글링을 열심히 하며,
매번 같은 것을 찾는 나자신을 보며 또 적어두기로 다짐했다.
위와같이 공공데이터 요청시 아래와같은 json 데이터가 return된다.
{
"response": {
"header": {
"resultCode": "00",
"resultMsg": "NORMAL SERVICE."
},
"body": {
"items": {
"item": [
{
"airline": "아시아나항공",
"airport": "로스앤젤레스",
"airportCode": "LAX",
"carousel": 18,
"cityCode": "LAX",
"elapsetime": 1204,
"estimatedDateTime": "0328",
"exitnumber": "D",
"flightId": "OZ203",
"gatenumber": 43,
"remark": "도착",
"scheduleDateTime": "0500",
"terminalId": "P01"
},
{
"airline": "란항공",
"airport": "로스앤젤레스",
"airportCode": "LAX",
"carousel": 6,
"cityCode": "LAX",
"elapsetime": 1215,
"estimatedDateTime": "0401",
"exitnumber": "B",
"flightId": "LA8425",
"gatenumber": 266,
"remark": "도착",
"scheduleDateTime": "0500",
"terminalId": "P03"
}
]
}
}
}
}
GSON으로 JSON객체 변환하기
String str = sb.toString();
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(str);
JsonObject rootob = element.getAsJsonObject().get("response").getAsJsonObject();
JsonObject body = rootob.getAsJsonObject().get("body").getAsJsonObject();
JsonObject items = body.getAsJsonObject().get("items").getAsJsonObject();
위와같은 소스로 받아온 데이터를 json객체로 파싱해준다.
List객체로 변환할때
Gson gson = new Gson();
JsonArray item = items.getAsJsonObject().get("item").getAsJsonArray();
List<ReturnVo> list2 = gson.fromJson(item.toString(), new TypeToken<List<ReturnVo>>(){}.getType());
단일객체로 변환할때
Gson gson = new Gson();
ReturnVo item = gson.fromJson(items, ReturnVo.class);
ReturnVo
public class ReturnVo{
private static final long serialVersionUID = -1846931839175019298L;
private List<AirItem> item;
public List<AirItem> getItem() {
return item;
}
public void setItem(List<AirItem> item) {
this.item = item;
}
public static class AirItem {
/** 항공사 */
private String airline;
/** 편명 */
private String flightId;
/** 출발공항코드 */
private String airportCode;
/** 출발예정시간 */
private String scheduleDateTime;
/** 출발변경시간 */
private String estimatedDateTime;
/** 출발 or 도착 공항 */
private String airport;
/** 탑승구 */
private String gatenumber;
/** 체크인카운터 */
private String chkinrange;
/** 수하물수취대 */
private String carousel;
/** */
private String cityCode;
/** 소요시간 */
private String elapsetime;
/** 1경유지 */
private String firstopover;
/** 1경유지이름 */
private String firstopovername;
/** 2경유지 */
private String secstopover;
/** 2경유지이름 */
private String secstopovername;
/** 3경유지 */
private String thistopover;
/** 3경유지이름 */
private String thistopovername;
/** 4경유지 */
private String foustopover;
/** 4경유지이름 */
private String foustopovername;
/** 5경유지 */
private String fifstopover;
/** 5경유지이름 */
private String fifstopovername;
/** 6경유지 */
private String sixstopover;
/** 6경유지이름 */
private String sixstopovername;
/** 출구 */
private String exitnumber;
/** 현황 */
private String remark;
/** 터미널아이디 */
private String terminalId;
public String getAirline() {
return airline;
}
public void setAirline(String airline) {
this.airline = airline;
}
public String getAirport() {
return airport;
}
public void setAirport(String airport) {
this.airport = airport;
}
public String getAirportCode() {
return airportCode;
}
public void setAirportCode(String airportCode) {
this.airportCode = airportCode;
}
public String getCarousel() {
return carousel;
}
public void setCarousel(String carousel) {
this.carousel = carousel;
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
public String getElapsetime() {
return elapsetime;
}
public void setElapsetime(String elapsetime) {
this.elapsetime = elapsetime;
}
public String getEstimatedDateTime() {
return estimatedDateTime;
}
public void setEstimatedDateTime(String estimatedDateTime) {
this.estimatedDateTime = estimatedDateTime;
}
public String getExitnumber() {
return exitnumber;
}
public void setExitnumber(String exitnumber) {
this.exitnumber = exitnumber;
}
public String getFlightId() {
return flightId;
}
public void setFlightId(String flightId) {
this.flightId = flightId;
}
public String getGatenumber() {
return gatenumber;
}
public void setGatenumber(String gatenumber) {
this.gatenumber = gatenumber;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getScheduleDateTime() {
return scheduleDateTime;
}
public void setScheduleDateTime(String scheduleDateTime) {
this.scheduleDateTime = scheduleDateTime;
}
public String getTerminalId() {
return terminalId;
}
public void setTerminalId(String terminalId) {
this.terminalId = terminalId;
}
public String getChkinrange() {
return chkinrange;
}
public void setChkinrange(String chkinrange) {
this.chkinrange = chkinrange;
}
public String getFirstopover() {
return firstopover;
}
public void setFirstopover(String firstopover) {
this.firstopover = firstopover;
}
public String getFirstopovername() {
return firstopovername;
}
public void setFirstopovername(String firstopovername) {
this.firstopovername = firstopovername;
}
public String getSecstopover() {
return secstopover;
}
public void setSecstopover(String secstopover) {
this.secstopover = secstopover;
}
public String getSecstopovername() {
return secstopovername;
}
public void setSecstopovername(String secstopovername) {
this.secstopovername = secstopovername;
}
public String getThistopover() {
return thistopover;
}
public void setThistopover(String thistopover) {
this.thistopover = thistopover;
}
public String getThistopovername() {
return thistopovername;
}
public void setThistopovername(String thistopovername) {
this.thistopovername = thistopovername;
}
public String getFoustopover() {
return foustopover;
}
public void setFoustopover(String foustopover) {
this.foustopover = foustopover;
}
public String getFoustopovername() {
return foustopovername;
}
public void setFoustopovername(String foustopovername) {
this.foustopovername = foustopovername;
}
public String getFifstopover() {
return fifstopover;
}
public void setFifstopover(String fifstopover) {
this.fifstopover = fifstopover;
}
public String getFifstopovername() {
return fifstopovername;
}
public void setFifstopovername(String fifstopovername) {
this.fifstopovername = fifstopovername;
}
public String getSixstopover() {
return sixstopover;
}
public void setSixstopover(String sixstopover) {
this.sixstopover = sixstopover;
}
public String getSixstopovername() {
return sixstopovername;
}
public void setSixstopovername(String sixstopovername) {
this.sixstopovername = sixstopovername;
}
}
}
참고적으로 json을 객체로 변환할때 만든 객체는 위와같다.
'개발' 카테고리의 다른 글
이클립스 tomcat could not create the java virtual machine 오류 (0) | 2022.09.15 |
---|---|
[jquery handlebars] 헬퍼함수 다중조건 if문 사용하기 (0) | 2022.02.11 |
[공공데이터] 인천공항 API 호출시 500 오류 (0) | 2021.10.22 |
[Python] json dumps로 예쁘게 출력하기 (0) | 2021.10.05 |
티스토리 API - Python으로 글 목록 불러오기 (0) | 2021.10.04 |
댓글