Java 为 Kotlin 创建 POJO 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44180346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:24:14  来源:igfitidea点击:

Create POJO Class for Kotlin

javaandroidgsonkotlinpojo

提问by Pratik Butani

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

我想为 Kotlin 创建 POJO 类,因为我们知道www.jsonschema2pojo.org将 JSON 转换为 POJO,因此我们可以将它与 gson 一起使用。

Anyone know how to create Gson POJO for Kotlin QUICKLY?

有人知道如何快速为 Kotlin 创建 Gson POJO吗?

Edited:

编辑:

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

我知道它使用 Data 类,但是有没有最简单的方法来创建它?

采纳答案by Pratik Butani

Yes, I got solution

是的,我得到了解决方案

for Example:

例如:

{
    "foo": "string",
    "bar": "integer",
    "baz": "boolean"
}

My POJO Class Created using http://www.jsonschema2pojo.org/

我的 POJO 类使用http://www.jsonschema2pojo.org/创建

Example.java

例子.java

public class Example {

    @SerializedName("foo")
    @Expose
    private String foo;
    @SerializedName("bar")
    @Expose
    private String bar;
    @SerializedName("baz")
    @Expose
    private String baz;

    public String getFoo() {
        return foo;
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }

    public String getBar() {
        return bar;
    }

    public void setBar(String bar) {
        this.bar = bar;
    }

    public String getBaz() {
        return baz;
    }

    public void setBaz(String baz) {
        this.baz = baz;
    }
}

Converted KotlinClass using Code -> Convert Java File to Kotlin Fileor CTRL + ALT + SHIFT + K

使用或转换KotlinCode -> Convert Java File to Kotlin FileCTRL + ALT + SHIFT + K

Example.kt

例子.kt

class Example {

    @SerializedName("foo")
    @Expose
    var foo: String? = null
    @SerializedName("bar")
    @Expose
    var bar: String? = null
    @SerializedName("baz")
    @Expose
    var baz: String? = null
}

Thank you all.

谢谢你们。

回答by PRATEEK BHARDWAJ

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

回答by MatPag

A feature request about Kotlin support to auto generate data classes have been filled herein jsonschema2pojo github repository. Currently, there is no jsonschema2kotlin web utility available.

关于自动科特林支持某种功能的请求产生的数据类已经充满这里的jsonschema2pojo GitHub的仓库。目前,没有可用的 jsonschema2kotlin Web 实用程序。

If you don't have any problem installing a new plugin on Android Studio, follow the accepted answer, otherwise the best you can do is to use jsonschema2pojoto convert JSON to Java POJO and the use the Android Studio 3.0+ feature that converts a Java file to a Kotlin one.

如果您在 Android Studio 上安装新插件没有任何问题,请按照已接受的答案进行操作,否则您能做的最好的事情就是使用jsonschema2pojo将 JSON 转换为 Java POJO,并使用 Android Studio 3.0+ 功能转换 Java文件到 Kotlin 文件。

enter image description here

enter image description here

回答by Nabin Khatiwada

If I got your question, you might be searching some plugin for converting to POJO. So RoboPOJOGeneratormay help you. You can use a plugin from File>Setting>Plugin>Browse Repositoriesand 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 gsonlibrary in gradlebecause this plugin will automatically generate annotation of gsonlike @SerializedName, etc.

如果我收到您的问题,您可能正在搜索一些插件以转换为 POJO。所以 RoboPOJOGenerator可以帮到你。您可以使用插件 fromFile>Setting>Plugin>Browse Repositories并搜索RoboPOJOGenerator. 要使用此插件,您首先需要创建一个单独的包,如“data”,右键单击该包,您将看到 Generate POJO from JSON. 此外,您需要在其中包含gson库,gradle因为此插件会自动生成gsonlike@SerializedName等注释。

回答by u5167396

I think this should be the Plugin what you want

我认为这应该是你想要的插件

JSON To Kotlin Class Plugin

JSON To Kotlin Class Plugin

https://github.com/wuseal/JsonToKotlinClass

https://github.com/wuseal/JsonToKotlinClass

回答by Kishan Solanki

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

Unbelievable Right! Its as simple as that. Just use datakeyword before classto create Data class in Kotlin.

难以置信的对!就这么简单。只需使用data关键字 beforeclass在 Kotlin 中创建 Data 类。

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.

Data 类为您提供了一切,getter、setter、hashCode、toString 和 equals 函数。所以你所要做的就是创建一个实例并开始使用这些函数。

回答by Sunil

Try this

尝试这个

This is the simple way

这是简单的方法

  1. Right click on the package name and select New->Kotlin File/Classenter image description here
  2. Name the name, In my case, I am naming this as Model you can whatever you like and click on okenter 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

  1. 右键单击包名并选择New->Kotlin File/Classenter image description here
  2. 命名名称,就我而言,我将其命名为模型,您可以随心所欲,然后单击确定enter image description here
  3. 并粘贴此代码,这是您的 POJO/Model 类

    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

enter image description here

回答by Manish Gowardipe

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

在 vs-code 中有一个名为 Paste JSON as Code 的插件。它支持多种语言。 将 Json 粘贴为代码

quick look

quick look