Challenge - 5 Problems
JSON Parsing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2: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)
Attempts:
2 left
💡 Hint
Check how Gson maps JSON keys to data class properties.
✗ Incorrect
Gson maps JSON keys to matching Kotlin data class properties by name. The JSON key "name" matches the property
name, so user.name is "Alice".📝 Syntax
intermediate2:00remaining
Which Moshi adapter code correctly parses a JSON array of strings?
You want to parse this JSON array: . Which Moshi adapter code is correct?
["apple", "banana", "cherry"] into a Kotlin ListAttempts:
2 left
💡 Hint
Moshi needs a parameterized type for generic collections.
✗ Incorrect
Moshi requires a parameterized type to parse generic collections like List. Using Types.newParameterizedType creates the correct type for the adapter.
❓ lifecycle
advanced2: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)
Attempts:
2 left
💡 Hint
Check how Gson handles malformed JSON strings.
✗ Incorrect
Gson throws JsonSyntaxException when the JSON string is malformed, such as missing quotes around string values.
🧠 Conceptual
advanced2: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)
Attempts:
2 left
💡 Hint
Think about how JSON keys and Kotlin properties can have different names.
✗ Incorrect
The @Json annotation tells Moshi to map the JSON key
user_name to the Kotlin property userName, enabling correct parsing despite naming differences.🔧 Debug
expert3: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)
Attempts:
2 left
💡 Hint
Check what happens when a JSON key is missing for a nullable property.
✗ Incorrect
The JSON does not include the address key, so Moshi sets the address property to null. Accessing
address!!.city causes a NullPointerException because address is null.