본문 바로가기

개인 프로젝트

[Project] 영화 데이터베이스 웹 페이지 제작 ( 2 )

영화 데이터가 정상적으로 출력되는 것을 확인했으니 

이 데이터를 가공(?)해서 내가 필요한 부분만 사용하기 위해 JSON 데이터를 파싱할 것이다

 

Java에서 제공하는 org.json 라이브러리를 사용할거임

 

빌드툴은 Kotlin Gradle DSL 을 사용하고 있으므로 

build.gradle.kts 파일에 의존성를 하자

 

사실 Kotlin 과 Groovy의 차이는 모르겠다 나중에 공부하고 정리해서 글 올려야겠다 

 

아무튼 의존성 추가를 해야하는데 내가 여기서 너무너무너무 바보같은 실수를 해서 거의 3시간을 통으로 날려버렸다

 

dependencies에 implementation("org.json:json:20210307") 을 추가해야 하는데 

기존 코드에 복사 붙여넣기를 하다보니 implementation이 아닌 testImplementation을 사용해버린 것이다

나는 그것도 모르고 왜 import가 안되지 한참을 인터넷을 뒤적거리고 GPT한테 쓴소리해버렸다

GPT야 미안해~

 

testImplementation은 테스트 코드에서만 필요한 라이브러리를 추가할 때 사용되기 때문에 내 코드에는 적용이 안된다

 

하....

 

 

실패는 성공의 어머니라고 이번 실수를 통해 다음에는 똑같은 실수를 반복하지 말아야겠다

 

아무쪼록 그렇게 의존성 추가를 끝내고나면 

이 두개를 import 하여 사용할 수 있다

 

우선 Api로 가져온 데이터가 어떻게 생겼나 확인해보자

 

{

"Query":"봉준호",

"KMAQuery":"봉준호",

"TotalCount":62,

"Data":[{"CollName":"kmdb_new2","TotalCount":62,"Count":10,
"Result":[{
"DOCID":"K05731",
"movieId":"K",
"movieSeq":"05731",
"title":" 지리멸렬",
"titleEng":"Incoherence (Ji-li-myeol-lyeol)",
"titleOrg":"",
"titleEtc":"지리멸렬^지리 멸렬^Incoherence (Ji-li-myeol-lyeol)^支離滅裂",
"prodYear":"1994",
"directors":{"director":[{"directorNm":" !HS 봉준호 !HE ","directorEnNm":"Bong Joon-ho","directorId":"00001843"}]},

 

 

뭐 대충 이렇게 생겼다. 

 

내가 사용할 데이터는 영화이름, 감독명, 줄거리, 장르, 개봉날짜, 키워드,포스터 대충 이정도? 

 

JSONObject jsonResponse = new JSONObject(sb.toString());
        JSONArray dataArray = jsonResponse.getJSONArray("Data");
        if (dataArray.length() > 0) {
            JSONObject dataObject = dataArray.getJSONObject(0);
            JSONArray resultArray = dataObject.getJSONArray("Result");
            for (int i = 0; i < resultArray.length(); i++) {
                JSONObject movie = resultArray.getJSONObject(i);
                String title = movie.getString("title"); //영화제목
                JSONObject directors = movie.getJSONObject("directors");
                JSONArray directorArray = directors.getJSONArray("director");
                String directorName = directorArray.getJSONObject(0).getString("directorNm"); //감독이름
                JSONObject plots = movie.getJSONObject("plots");
                JSONArray plotArray = plots.getJSONArray("plot");
                String plotText = plotArray.getJSONObject(0).getString("plotText"); //줄거리
                JSONObject ratings = movie.getJSONObject("ratings");
                JSONArray ratingArray = ratings.getJSONArray("rating");
                String releaseDate = ratingArray.getJSONObject(0).getString("releaseDate"); //개봉날짜
                String genre = movie.getString("genre");
                String keywords = movie.getString("keywords");


                System.out.println("영화 제목: " + title);
                System.out.println("감독 이름: " + directorName);
                System.out.println("줄거리: " + plotText);
                System.out.println("장르: "+genre);
                System.out.println("키워드: "+keywords);
                System.out.println("개봉날짜: "+releaseDate);
                System.out.println();
            }
        }

 

결과를 확인해보자

 

 

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

 

 

 

아주 이쁘게 출력되었다

 

이제 이 가공된 데이터들을 데이터베이스에서 관리해보자