Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Serialize and Deserialize JSON using GSON Library in Android

  • Last Updated : 06 Dec, 2021

When we need to send some data from one activity to another, we can use intents with putExtra() function. But we putExtra supports primitive data types. So if we need to pass and the user defines the object, we need to serialize it first, then send it to the destination. While receiving the data we need to deserialize it. We will serialize and deserialize objects to JSON using the Gson library.

Step by step Implementation

Step 1: Create a new project in android studio using kotlin.

Step 2: Create a user-defined data class

Kotlin




package com.ayush.serialize_deserialize
  
data class StudentInfo(val id: Int, val Name :String, val RollNo : Int)

Step 3: Create a new activity where we will send the data

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".SecondActivity">
    <TextView
        android:id="@+id/tvId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ID"
        android:textStyle="bold"
        android:textColor="#7CB342"
        android:textSize="22sp"/>
    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="NAME"
        android:textStyle="bold"
        android:textColor="#7CB342"
        android:layout_margin="4dp"
        android:textSize="22sp"/>
    <TextView
        android:id="@+id/tvRoll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ROLL"
        android:textStyle="bold"
        android:textColor="#7CB342"
        android:textSize="22sp"/>
</LinearLayout>

Step 4: Now we will create an object of user define class, and convert it into JSON data using Gson().toJson() function and send it to the intent.

Kotlin




package com.ayush.serialize_deserialize
  
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.gson.Gson
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        val stu01 = StudentInfo(1,"AMAN",10)
        val json = Gson().toJson(stu01)
        val intent = Intent(this,SecondActivity::class.java)
        intent.putExtra("data",json)
        startActivity(intent)
    }
}

Step 5: We will extract data from intent using getExtra() method and deserialize it using Gson().fromJson().

Kotlin




package com.ayush.serialize_deserialize
  
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import com.google.gson.Gson
  
class SecondActivity : AppCompatActivity() {
    lateinit var data : StudentInfo
    lateinit var tvId : TextView
    lateinit var tvName : TextView
    lateinit var tvRoll : TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
  
        tvId = findViewById(R.id.tvId)
        tvName = findViewById(R.id.tvName)
        tvRoll= findViewById(R.id.tvRoll)
  
       val json = intent.getStringExtra("data")
        data = Gson().fromJson(json,StudentInfo::class.java)
  
        tvId.text = data.id.toString()
        tvName.text = data.Name
        tvRoll.text = data.RollNo.toString()
    }
}

So, our app is ready and we can see the output.

Output:

Output


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!