JSON mit Moshi parsen
Überführen Sie Antworten in Kotlin-Data-Classes.
JSON mit Moshi parsen ist eine kostenlose Jetpack Compose Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Jetpack Compose Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.
Häufig gestellte Fragen
Ist die Lektion „JSON mit Moshi parsen“ kostenlos?
Ja — der vollständige Text von „JSON mit Moshi parsen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Jetpack Compose Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „JSON mit Moshi parsen“?
Überführen Sie Antworten in Kotlin-Data-Classes. Du übst Jetpack Compose Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Jetpack Compose Academy zu starten?
Keine Vorkenntnisse erforderlich. Jetpack Compose Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „JSON mit Moshi parsen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Jetpack Compose Academy-Lektion Code schreiben und ausführen?
Ja. Jede Jetpack Compose Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Ein Retrofit-API-Interface definieren
- JSON mit Moshi parsen
- suspend-Aufrufe im ViewModel
- UI für Laden, Erfolg und Fehler