2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AndroidStudioのプラグインを使用してJSONをKotlinのデータクラスに一瞬で変換する

Posted at

AndroidStudioのプラグインを使用する事があまりなかったのですが、便利な機能があるので最近調べています。

APIのレスポンスデータを参考にデータクラス実装するのは単純作業なので、面倒ですよね。

AndroidStudioのプラグインを使用してJsonをKotlinのデータクラスに一瞬で変換してみようと思います。

変換方法

プラグインの入れ方はこちらを参考にしてください。

こちらを使用します

スクリーンショット 2024-03-31 17.49.30.png

こちらをインストールいたします。

使用方法

スクリーンショット 2024-03-31 18.04.07.png

普段クラスを作成するときと同じで右クリックをすると画像のようなものが増えてます。

画像のような画面が表示されるので、変換するJSONを入力します。

スクリーンショット 2024-03-31 18.28.15.png

Class Nameに作成するデータクラス名を入力します。

Advancedをクリックすると詳細設定ができます。

スクリーンショット 2024-03-31 18.05.15.png

スクリーンショット 2024-03-31 18.05.22.png

今回は@Keep @Serializable の設定しています。

出来上がったデータクラスはこちらです。

User.kt
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import androidx.annotation.Keep

@Keep
@Serializable
data class User(
    @SerialName("member")
    val member: Boolean,
    @SerialName("user_id")
    val userId: Int,
    @SerialName("user_name")
    val userName: String
)

簡単に作成できました!

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?