Create POJO Class for Kotlin

Viewed 51047

I want to create POJO class for Kotlin, as we know that www.jsonschema2pojo.org converts JSON to POJO so we can use it with gson.

Anyone know how to create Gson POJO for Kotlin QUICKLY?

Edited:

I know its use Data classes, but is there any simplest way to create it?

10 Answers
data class ModelUser(val imagePath: String,val userName: String)

Unbelievable Right! Its as simple as that. Just use data keyword before class to create Data class in Kotlin.

Data class provides you with everything, getters, setters, hashCode, toString and equals functions. So all you have to do is create an instance and start using the functions.

In vs-code there is a plugin named Paste JSON as Code. it supports many languages. Paste Json as code

quick look

Try this

This is the simple way

  1. Right click on the package name and select New->Kotlin File/Class enter image description here
  2. Name the name, In my case, I am naming this as Model you can whatever you like and click on ok enter image description here
  3. and Paste this code, This is you POJO/Model class

    class Model {
        var uid: String? = null
        var name: String? = null
    }
    

    enter image description here

How to use this

 val model=Model()
 model.name="Sunil"
 Log.e("Model after",model.name)

enter image description here

Use the Android Studio or IntelliJ IDEA plugin: JSON To Kotlin Class ​(JsonToKotlinClass)​

In my case Code -> Generate doesn't work, it is disabled (see screenshot)

enter image description here

You should install the plugin "JsonToKotlinClass"

Install plugin for Android Studio

Then right-click on the namespace and select

Then right-click on the namespace and select

Paste your JSON here. That's all, profit.

enter image description here

Related