MoshiでJSONを解析する
レスポンスをKotlinのデータクラスにマッピングします。
「MoshiでJSONを解析する」はCoddyKit上の無料Jetpack Compose Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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時間対応のAIチューター)、Jetpack Compose Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Jetpack Compose Academyコースには全4レッスンが含まれています。
「MoshiでJSONを解析する」で何を学びますか?
レスポンスをKotlinのデータクラスにマッピングします。 ブラウザで直接実行するハンズオンコードでJetpack Compose Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Jetpack Compose Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのJetpack Compose Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「MoshiでJSONを解析する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このJetpack Compose Academyレッスンでコードを書いて実行できますか?
はい。すべてのJetpack Compose Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。