Skip to content

Commit 58ea476

Browse files
committed
강수 예보 JSON 파싱 로직 개선: 4-7일 AM/PM 분리, 8-10일 통합 처리
- WeatherApiClient: parsePrecipitationDataFromJson 함수 수정 - 4~7일: rnSt4Am/Pm, wf4Am/Pm 형태로 오전/오후 분리 파싱 - 8~10일: rnSt8, wf8 형태로 통합 데이터 파싱 - 테스트 코드와 동일한 파싱 로직으로 통일
1 parent fdcf151 commit 58ea476

File tree

3 files changed

+54
-22
lines changed

3 files changed

+54
-22
lines changed

src/main/kotlin/com/back/koreaTravelGuide/domain/weather/client/WeatherApiClient.kt

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,38 @@ class WeatherApiClient(
109109
val precipitationData = PrecipitationData()
110110

111111
for (day in 4..10) {
112-
val amRain = (extractJsonValue(jsonResponse, "response.body.items.item[0].rnSt${day}Am") as? Number)?.toInt()
113-
val pmRain = (extractJsonValue(jsonResponse, "response.body.items.item[0].rnSt${day}Pm") as? Number)?.toInt()
114-
val amWeather = extractJsonValue(jsonResponse, "response.body.items.item[0].wf${day}Am") as? String
115-
val pmWeather = extractJsonValue(jsonResponse, "response.body.items.item[0].wf${day}Pm") as? String
116-
117-
if (amRain != null || pmRain != null || !amWeather.isNullOrBlank() || !pmWeather.isNullOrBlank()) {
118-
val precipInfo = PrecipitationInfo(
119-
amRainPercent = amRain,
120-
pmRainPercent = pmRain,
121-
amWeather = amWeather,
122-
pmWeather = pmWeather
123-
)
124-
125-
precipitationData.setDay(day, precipInfo)
112+
if (day <= 7) {
113+
// 4~7일: 오전/오후 구분
114+
val amRain = (extractJsonValue(jsonResponse, "response.body.items.item[0].rnSt${day}Am") as? Number)?.toInt()
115+
val pmRain = (extractJsonValue(jsonResponse, "response.body.items.item[0].rnSt${day}Pm") as? Number)?.toInt()
116+
val amWeather = extractJsonValue(jsonResponse, "response.body.items.item[0].wf${day}Am") as? String
117+
val pmWeather = extractJsonValue(jsonResponse, "response.body.items.item[0].wf${day}Pm") as? String
118+
119+
if (amRain != null || pmRain != null || !amWeather.isNullOrBlank() || !pmWeather.isNullOrBlank()) {
120+
val precipInfo = PrecipitationInfo(
121+
amRainPercent = amRain,
122+
pmRainPercent = pmRain,
123+
amWeather = amWeather,
124+
pmWeather = pmWeather
125+
)
126+
127+
precipitationData.setDay(day, precipInfo)
128+
}
129+
} else {
130+
// 8~10일: 통합 (오전/오후 구분 없음)
131+
val rainPercent = (extractJsonValue(jsonResponse, "response.body.items.item[0].rnSt$day") as? Number)?.toInt()
132+
val weather = extractJsonValue(jsonResponse, "response.body.items.item[0].wf$day") as? String
133+
134+
if (rainPercent != null || !weather.isNullOrBlank()) {
135+
val precipInfo = PrecipitationInfo(
136+
amRainPercent = rainPercent,
137+
pmRainPercent = null,
138+
amWeather = weather,
139+
pmWeather = null
140+
)
141+
142+
precipitationData.setDay(day, precipInfo)
143+
}
126144
}
127145
}
128146

src/test/kotlin/com/back/koreaTravelGuide/WeatherApiRealTest.kt

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,27 @@ class WeatherApiRealTest {
177177
println("✅ 강수 확률 데이터 (4일~10일):")
178178
println("----------------------------------------")
179179
for (day in 4..10) {
180-
val amRain = firstItem?.get("rnSt${day}Am")
181-
val pmRain = firstItem?.get("rnSt${day}Pm")
182-
val amWeather = firstItem?.get("wf${day}Am")
183-
val pmWeather = firstItem?.get("wf${day}Pm")
184-
println("📅 ${day}일 후:")
185-
println(" 오전: $amWeather (강수확률: $amRain%)")
186-
println(" 오후: $pmWeather (강수확률: $pmRain%)")
180+
if (day <= 7) {
181+
// 4~7일: 오전/오후 구분
182+
val amRain = firstItem?.get("rnSt${day}Am")
183+
val pmRain = firstItem?.get("rnSt${day}Pm")
184+
val amWeather = firstItem?.get("wf${day}Am")
185+
val pmWeather = firstItem?.get("wf${day}Pm")
186+
187+
if (amRain != null || pmRain != null || amWeather != null || pmWeather != null) {
188+
println("📅 ${day}일 후:")
189+
println(" 오전: ${amWeather ?: "정보없음"} (강수확률: ${amRain ?: 0}%)")
190+
println(" 오후: ${pmWeather ?: "정보없음"} (강수확률: ${pmRain ?: 0}%)")
191+
}
192+
} else {
193+
// 8~10일: 통합 (오전/오후 구분 없음)
194+
val rainPercent = firstItem?.get("rnSt$day")
195+
val weather = firstItem?.get("wf$day")
196+
197+
if (rainPercent != null || weather != null) {
198+
println("📅 ${day}일 후: ${weather ?: "정보없음"} (강수확률: ${rainPercent ?: 0}%)")
199+
}
200+
}
187201
}
188202
println()
189203
}

src/test/resources/application-test.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
spring.config.activate.on-profile=test
33

44
# Weather API Configuration
5-
weather.api.key=?? api key? ?????
5+
weather.api.key=?? api key? ??
66
weather.api.base-url=https://apis.data.go.kr/1360000/MidFcstInfoService
77

88
# Tour API Configuration

0 commit comments

Comments
 (0)