Create POJO Class for Kotlin

前端 未结 10 1902
闹比i
闹比i 2021-02-03 18:34

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

相关标签:
10条回答
  • 2021-02-03 19:07
    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
    
    0 讨论(0)
  • 2021-02-03 19:16

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

    0 讨论(0)
  • 2021-02-03 19:17

    Try this

    This is the simple way

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

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

    How to use this

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

    0 讨论(0)
  • 2021-02-03 19:18
    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.

    0 讨论(0)
  • 2021-02-03 19:19

    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.

    0 讨论(0)
  • 2021-02-03 19:27

    I think this should be the Plugin what you want

    https://github.com/wuseal/JsonToKotlinClass

    0 讨论(0)
提交回复
热议问题