0
0
Android Kotlinmobile~20 mins

JSON parsing with Gson/Moshi in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSON Parsing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output of this Gson parsing code?
Given this Kotlin data class and Gson parsing code, what will be the value of user.name after parsing?
Android Kotlin
data class User(val id: Int, val name: String)

val json = "{\"id\":1, \"name\":\"Alice\"}"
val user = Gson().fromJson(json, User::class.java)
println(user.name)
A"Alice"
BThrows JsonSyntaxException
Cnull
D"id"
Attempts:
2 left
💡 Hint
Check how Gson maps JSON keys to data class properties.
📝 Syntax
intermediate
2:00remaining
Which Moshi adapter code correctly parses a JSON array of strings?
You want to parse this JSON array: ["apple", "banana", "cherry"] into a Kotlin List. Which Moshi adapter code is correct?
A
val moshi = Moshi.Builder().build()
val adapter = moshi.adapter<List<String>>(String::class.java)
val fruits = adapter.fromJson(json)
B
val moshi = Moshi.Builder().build()
val adapter = moshi.adapter<List<String>>(List::class.java)
val fruits = adapter.fromJson(json)
C
val moshi = Moshi.Builder().build()
val type = Types.newParameterizedType(List::class.java, String::class.java)
val adapter = moshi.adapter<List<String>>(type)
val fruits = adapter.fromJson(json)
D
val moshi = Moshi.Builder().build()
val adapter = moshi.adapter<List<String>>()
val fruits = adapter.fromJson(json)
Attempts:
2 left
💡 Hint
Moshi needs a parameterized type for generic collections.
lifecycle
advanced
2:00remaining
What happens if you parse invalid JSON with Gson?
Consider this code snippet parsing invalid JSON with Gson. What is the expected behavior?
Android Kotlin
val json = "{\"id\": 1, \"name\": Alice}" // Missing quotes around Alice
val user = Gson().fromJson(json, User::class.java)
AReturns a User object with name set to null
BThrows com.google.gson.JsonSyntaxException
CReturns a User object with name set to "Alice"
DReturns null without exception
Attempts:
2 left
💡 Hint
Check how Gson handles malformed JSON strings.
🧠 Conceptual
advanced
2:00remaining
Why use @Json annotation in Moshi data classes?
You have a JSON key named user_name but your Kotlin property is userName. Why use @Json(name = "user_name") annotation?
Android Kotlin
data class User(@Json(name = "user_name") val userName: String)
ATo ignore the <code>user_name</code> key during parsing
BTo rename the Kotlin property to <code>user_name</code> in the compiled code
CTo convert the property to uppercase during parsing
DTo map the JSON key <code>user_name</code> to the Kotlin property <code>userName</code>
Attempts:
2 left
💡 Hint
Think about how JSON keys and Kotlin properties can have different names.
🔧 Debug
expert
3:00remaining
Why does this Moshi parsing code throw a NullPointerException?
Given this Kotlin data class and Moshi parsing code, why does user.address.city cause a NullPointerException?
Android Kotlin
data class Address(val city: String)
data class User(val name: String, val address: Address?)

val json = "{\"name\":\"Bob\"}" // address key missing
val moshi = Moshi.Builder().build()
val adapter = moshi.adapter(User::class.java)
val user = adapter.fromJson(json)
println(user!!.address!!.city)
ABecause the JSON is missing the address key, so address is null and accessing city causes NullPointerException
BBecause Moshi cannot parse nested data classes
CBecause the city property is not nullable
DBecause the adapter is not built correctly
Attempts:
2 left
💡 Hint
Check what happens when a JSON key is missing for a nullable property.