0
0
Android Kotlinmobile~5 mins

JSON parsing with Gson/Moshi in Android Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is Gson in Android development?
Gson is a library that helps convert JSON strings into Kotlin objects and vice versa. It makes handling JSON easy and automatic.
Click to reveal answer
beginner
What is Moshi and how is it different from Gson?
Moshi is another JSON library for Android that is modern and supports Kotlin features better, like null safety and Kotlin data classes. It is often preferred for Kotlin projects.
Click to reveal answer
beginner
How do you parse a JSON string into a Kotlin data class using Gson?
You create a Gson object and call fromJson(jsonString, YourDataClass::class.java). This converts the JSON string into your Kotlin object.
Click to reveal answer
intermediate
What annotation does Moshi use to map JSON keys to Kotlin properties?
Moshi uses the @Json(name = "json_key") annotation to map JSON keys to Kotlin properties when the names differ.
Click to reveal answer
beginner
Why is it important to use data classes with Moshi or Gson?
Data classes automatically provide useful functions like equals(), hashCode(), and toString(), and they work well with JSON libraries for easy parsing and serialization.
Click to reveal answer
Which library is known for better Kotlin support and null safety in JSON parsing?
AMoshi
BRetrofit
CJackson
DGson
How do you convert a JSON string to a Kotlin object using Gson?
AMoshi().fromJson(jsonString, DataClass::class.java)
BGson().toJson(jsonString, DataClass::class.java)
CGson().fromJson(jsonString, DataClass::class.java)
DJsonParser.parse(jsonString)
Which annotation is used in Moshi to rename JSON keys?
A@SerializedName
B@Json(name = "key")
C@JsonKey
D@JsonProperty
What Kotlin feature helps Moshi parse JSON easily?
AExtension functions
BSealed classes
CCompanion objects
DData classes
Which of these is NOT a function provided automatically by Kotlin data classes?
AparseJson()
Bequals()
CtoString()
DhashCode()
Explain how to parse a JSON string into a Kotlin object using Gson.
Think about the method that converts JSON text to an object.
You got /4 concepts.
    Describe the advantages of using Moshi over Gson in Kotlin projects.
    Focus on Kotlin-specific features and annotations.
    You got /4 concepts.