Complete the code to create a Gson instance for JSON parsing.
val gson = GsonBuilder().[1]().create()The setPrettyPrinting() method enables formatted JSON output when using Gson.
Complete the code to parse a JSON string into a Kotlin data class using Gson.
val user: User = gson.[1](jsonString, User::class.java)
The fromJson() method converts a JSON string into an object of the specified class.
Fix the error in this Moshi adapter creation code by filling the blank.
val moshi = Moshi.Builder().build() val adapter = moshi.adapter([1]::class.java)
The adapter method needs the class type, so User::class.java is correct. Using lowercase user is a variable, not a class.
Fill both blanks to parse a JSON array string into a list of User objects using Gson.
val listType = object : TypeToken<[1]<[2]>>() {}.type val users: List<User> = gson.fromJson(jsonArrayString, listType)
To parse a JSON array, we use List of User as the type token.
Fill all three blanks to create a Moshi adapter for a list of User objects and parse JSON.
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)
We use List as the parameterized type, and User as the class inside the list for Moshi parsing.