Skip to main content

How to convert JSON to Kotlin data classes using GSON in Kotlin Android

How to convert JSON to Kotlin data classes using GSON in Kotlin Android.

Here's a step-by-step tutorial on how to convert JSON to Kotlin data classes using GSON in Kotlin Android.

Step 1: Add GSON Dependency

To use GSON in your Kotlin Android project, you need to add the GSON dependency to your build.gradle file. Open your build.gradle (Module: app) file and add the following line inside the dependencies block:

implementation 'com.google.code.gson:gson:2.8.8'

Step 2: Create Kotlin Data Classes

Next, you need to create Kotlin data classes that represent the structure of your JSON data. Each data class will correspond to a JSON object or array.

For example, let's say you have the following JSON response:

{
"name": "John",
"age": 25,
"email": "[email protected]"
}

You can create a data class named User as follows:

data class User(
val name: String,
val age: Int,
val email: String
)

Step 3: Convert JSON to Kotlin Data Classes

Now, you can use GSON to convert the JSON to Kotlin data classes. Here's how you can do it:

val jsonString = """{
"name": "John",
"age": 25,
"email": "[email protected]"
}"""

val gson = Gson()
val user = gson.fromJson(jsonString, User::class.java)

In the above code, we first define a jsonString variable with the JSON data. Then, we create an instance of Gson. Finally, we use the fromJson method to convert the JSON string to a User object.

Step 4: Access the Data

Once you have converted the JSON to Kotlin data classes, you can access the data using the properties defined in the data class.

val name = user.name
val age = user.age
val email = user.email

In the above code, we are accessing the name, age, and email properties of the User object.

Step 5: Handling Nested Objects and Arrays

If your JSON data contains nested objects or arrays, you can represent them in your Kotlin data classes as well.

For example, consider the following JSON response:

{
"name": "John",
"age": 25,
"email": "[email protected]",
"address": {
"street": "123 Main St",
"city": "New York"
},
"friends": [
"Jane",
"Michael"
]
}

You can update the User data class to handle the nested objects and arrays:

data class User(
val name: String,
val age: Int,
val email: String,
val address: Address,
val friends: List<String>
)

data class Address(
val street: String,
val city: String
)

With the updated data classes, you can now convert the JSON to Kotlin data classes as before.

Step 6: Error Handling

When converting JSON to Kotlin data classes using GSON, it's important to handle any potential exceptions that may occur.

try {
val user = gson.fromJson(jsonString, User::class.java)
// Do something with the user object
} catch (e: JsonSyntaxException) {
// Handle JSON syntax exception
} catch (e: JsonParseException) {
// Handle JSON parse exception
} catch (e: Exception) {
// Handle other exceptions
}

In the above code, we use a try-catch block to catch any exceptions that may occur during the JSON conversion process. You can handle different types of exceptions based on your requirements.

That's it! You have now learned how to convert JSON to Kotlin data classes using GSON in Kotlin Android.