Kotlin & JPA
[Kotlin] Gradle json-simple 라이브러리 사용해서 json 파일 불러오기
Jane Kwon
2022. 1. 18. 13:36
반응형
json-simple 라이브러리를 사용하기 위해 build.gradle에 의존성을 주입해준다.
dependencies {
implementation("com.googlecode.json-simple:json-simple:1.1.1")
}
그런 다음 Gradle을 로드해주면 이렇게 라이브러리가 적용된 걸 확인할 수 있다.
이제 classpath 하위에 있는 json 파일을 불러오려면
ClassPathResource를 이용해 classpath의 기본경로인
src/main/java, src/main/resources에 첨부된 파일을 읽어올 수 있다.
fun readTemplate() {
val resource = ClassPathResource("json/templates.json")
val json: JSONObject = JSONParser().parse(InputStreamReader(resource.inputStream, "UTF-8")) as JSONObject
println(json)
}
readTemplate() 함수를 호출하면 콘솔에 아래와 같이 json 파일을 제대로 불러온 걸 알 수 있다.
{"templates": [{"type": "MX", "ttl": 180, "records": [{"content": "1 SERVER"}, {"content": "5 SERVER"}, {"content": "5 SERVER"}, {"content": "10 SERVER"}, {"content": "10 SERVER"}]}, {"type": "A", "ttl": 180, "records": [{"content": "IP"}]}]}
(참고 : https://12teamtoday.tistory.com/90)
반응형