Skip to main content

How to handle JSON arrays using GSON in Kotlin Android

How to handle JSON arrays using GSON in Kotlin Android.

Here's a step-by-step tutorial on how to handle JSON arrays using GSON in Kotlin for Android:

  1. Add GSON dependency to your project:

    • Open your app-level build.gradle file.
    • Inside the dependencies block, add the following line:
      implementation 'com.google.code.gson:gson:2.8.8'
    • Sync your project to download the GSON library.
  2. Create a data class to represent the JSON object:

    • Define a Kotlin data class that matches the structure of your JSON object.
    • Each property of the data class should correspond to a key-value pair in the JSON object.
    • For example, if your JSON object has properties like name, age, and email, your data class would look like this:
      data class Person(val name: String, val age: Int, val email: String)
  3. Parse JSON array using GSON:

    • Use GSON to convert the JSON array into a list of objects.
    • Create a Gson object:
      val gson = Gson()
    • Use the fromJson method to parse the JSON array into a list of objects:
      val jsonArray = "[{\"name\":\"John\", \"age\":30, \"email\":\"[email protected]\"}, {\"name\":\"Jane\", \"age\":25, \"email\":\"[email protected]\"}]"
      val personList = gson.fromJson(jsonArray, Array<Person>::class.java).toList()
  4. Access the data from the parsed JSON array:

    • Iterate over the list of objects to access individual items.
    • For example, to print the name and email of each person in the list:
      for (person in personList) {
      println("Name: ${person.name}")
      println("Email: ${person.email}")
      }
  5. Convert a list of objects to JSON array:

    • If you have a list of objects and want to convert it to a JSON array, use GSON.
    • Create a Gson object:
      val gson = Gson()
    • Use the toJson method to convert the list of objects to a JSON array:
      val personList = listOf(Person("John", 30, "[email protected]"), Person("Jane", 25, "[email protected]"))
      val jsonArray = gson.toJson(personList)
  6. Handle nested JSON arrays:

    • If your JSON array contains nested arrays, you can use GSON to parse them as well.
    • Define nested data classes to represent the structure of the nested arrays.
    • For example, if your JSON structure is like this:
      [
      {
      "name": "John",
      "friends": [
      {
      "name": "Jane",
      "age": 25,
      "email": "[email protected]"
      },
      {
      "name": "Tom",
      "age": 27,
      "email": "[email protected]"
      }
      ]
      },
      {
      "name": "Mary",
      "friends": [
      {
      "name": "Alex",
      "age": 29,
      "email": "[email protected]"
      }
      ]
      }
      ]
    • Create nested data classes to represent the structure:
      data class Friend(val name: String, val age: Int, val email: String)
      data class PersonWithFriends(val name: String, val friends: List<Friend>)

That's it! You now know how to handle JSON arrays using GSON in Kotlin for Android. You can use these steps to parse and manipulate JSON arrays in your own projects.