0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Android データクラスをJsonに変換する方法

Posted at

データクラスをJsonに変換する方法

serializationを使用するので、導入する必要があります。
導入しているプロジェクトが殆どだと思うので割愛します。

.kt
//必要なインポート
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json



val user = User(member = false, userId = 0, userName = "user")
val userJson = Json.encodeToString(user)

Json.encodeToStringの引数の値へJsonにしたいデータクラスを渡してあげるだけでできます。

userJsonの表示結果

{"member":false,"userId":0,"userName":"user"}

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?