0
0
Android Kotlinmobile~10 mins

JSON parsing with Gson/Moshi in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a Gson instance for JSON parsing.

Android Kotlin
val gson = GsonBuilder().[1]().create()
Drag options to blanks, or click blank then click option'
AserializeNulls
BtoJson
CfromJson
DsetPrettyPrinting
Attempts:
3 left
💡 Hint
Common Mistakes
Using toJson or fromJson here causes errors because they are Gson methods, not GsonBuilder.
2fill in blank
medium

Complete the code to parse a JSON string into a Kotlin data class using Gson.

Android Kotlin
val user: User = gson.[1](jsonString, User::class.java)
Drag options to blanks, or click blank then click option'
AparseJson
Bserialize
CfromJson
DtoJson
Attempts:
3 left
💡 Hint
Common Mistakes
Using toJson tries to convert an object to JSON, not parse JSON.
3fill in blank
hard

Fix the error in this Moshi adapter creation code by filling the blank.

Android Kotlin
val moshi = Moshi.Builder().build()
val adapter = moshi.adapter([1]::class.java)
Drag options to blanks, or click blank then click option'
AUser
BString
Cuser
DJsonAdapter
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name instead of a class causes compile errors.
4fill in blank
hard

Fill both blanks to parse a JSON array string into a list of User objects using Gson.

Android Kotlin
val listType = object : TypeToken<[1]<[2]>>() {}.type
val users: List<User> = gson.fromJson(jsonArrayString, listType)
Drag options to blanks, or click blank then click option'
AList
BUser
CArray
DString
Attempts:
3 left
💡 Hint
Common Mistakes
Using Array instead of List causes type mismatch errors.
5fill in blank
hard

Fill all three blanks to create a Moshi adapter for a list of User objects and parse JSON.

Android Kotlin
val moshi = Moshi.Builder().build()
val type = Types.newParameterizedType([1]::class.java, [2]::class.java)
val adapter = moshi.adapter<List<[3]>>(type)
val users = adapter.fromJson(jsonArrayString)
Drag options to blanks, or click blank then click option'
AList
BUser
CArray
DString
Attempts:
3 left
💡 Hint
Common Mistakes
Using Array instead of List or mismatching types causes runtime errors.