Moshi로 JSON 파싱하기
응답을 Kotlin 데이터 클래스로 매핑합니다.
Moshi로 JSON 파싱하기은(는) CoddyKit의 무료 Jetpack Compose Academy 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Jetpack Compose Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Jetpack Compose Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Why You Need a Parser
The server sends raw JSON text, but your UI needs real Kotlin objects. A parser like Moshi bridges that gap automatically.
Map JSON to a Data Class
Each JSON object becomes a Kotlin data class. Moshi matches field names and fills in the properties for you. ✨
data class User(
val id: Int,
val name: String
)Use @JsonClass for Codegen
Add @JsonClass with generateAdapter true so Moshi builds a fast adapter at compile time instead of using slow reflection.
@JsonClass(generateAdapter = true)
data class User(val id: Int, val name: String)Rename Fields With @Json
When a JSON key uses snake_case, the @Json annotation maps it to a clean Kotlin property name without renaming the API.
@Json(name = "full_name")
val fullName: StringNullable Means Optional
If a field might be missing, make it nullable with a question mark. Moshi sets it to null instead of crashing on absent keys.
data class User(
val id: Int,
val bio: String?
)Default Values Are Handy
Give a property a default value and Moshi uses it whenever the JSON omits that field, keeping your model resilient to change.
val isVerified: Boolean = falseNested Objects Just Work
If your JSON nests another object, model it as its own data class. Moshi parses the whole tree in one pass.
data class User(val id: Int, val address: Address)Lists of Objects
A JSON array maps cleanly to a Kotlin List of your data class. Moshi builds every item for you in order.
@GET("users")
fun getUsers(): Call<List<User>>Build a Moshi Instance
Create one shared Moshi object and add the Kotlin adapter factory so it understands data classes properly.
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()Plug Moshi Into Retrofit
Pass your Moshi instance to MoshiConverterFactory when building Retrofit, and every response is parsed without extra code.
.addConverterFactory(MoshiConverterFactory.create(moshi))Keep Models Focused
Only model the fields you actually use. Moshi ignores unknown JSON keys, so a lean data class stays clean and fast.
Quick Check
How do you map a JSON key named first_name to a Kotlin property called firstName?
Recap: JSON Into Objects
You modeled responses as data classes, handled renames, nulls, and nesting, and wired Moshi into Retrofit. Next you'll call these endpoints from a ViewModel.
자주 묻는 질문
“Moshi로 JSON 파싱하기” 강의는 무료인가요?
네 — “Moshi로 JSON 파싱하기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Jetpack Compose Academy 강의 전체를 잠금 해제할 수 있습니다. Jetpack Compose Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
“Moshi로 JSON 파싱하기”에서 뭘 배우나요?
응답을 Kotlin 데이터 클래스로 매핑합니다. 브라우저에서 직접 실행하는 실습 코드로 Jetpack Compose Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Jetpack Compose Academy을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Jetpack Compose Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“Moshi로 JSON 파싱하기” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Jetpack Compose Academy 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Jetpack Compose Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Retrofit API 인터페이스 정의하기
- Moshi로 JSON 파싱하기
- ViewModel의 suspend 호출
- 로딩, 성공 및 오류 UI