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
data class VideoGame(val name: String, val publisher: String, var reviewScore: Int)
//Constructor
val game: VideoGame = VideoGame("Gears of War", "Epic Games", 8)
print(game.name) // "Gears of War"
print(game.publisher) // "Epic Games"
print(game.reviewScore) // 8
game.reviewScore = 7
Use the Android Studio or IntelliJ IDEA plugin: JSON To Kotlin Class (JsonToKotlinClass)
This is the simple way
and Paste this code, This is you POJO/Model class
class Model {
var uid: String? = null
var name: String? = null
}
val model=Model()
model.name="Sunil"
Log.e("Model after",model.name)
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.
If I got your question, you might be searching some plugin for converting to POJO. So
RoboPOJOGenerator
may help you. You can use a plugin from File>Setting>Plugin>Browse Repositories
and search for RoboPOJOGenerator
.
To use this plugin you first need to create a separate package like "data", right-click the package and you will see Generate POJO from JSON
. Also, you need to include gson
library in gradle
because this plugin will automatically generate annotation of gson
like @SerializedName
, etc.
I think this should be the Plugin what you want
https://github.com/wuseal/JsonToKotlinClass