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?

More than 3 years have passed since last update.

kotlin-serialization

0
Last updated at Posted at 2022-02-20

Gradle設定追加

build.gradle
buildscript {
    dependencies {
        // Kotlin Serialization(追加)
        classpath "org.jetbrains.kotlin:kotlin-serialization:1.6.10"
    }
}
build.gradle(app)
plugins {
    
    // Kotlin Serialization(追加)
    id 'kotlinx-serialization'
}

dependencies {
    // Kotlin Serialization
    implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2'
}

Kotlin

Jsonに変換するクラス

UserInfo.kt
import kotlinx.serialization.Serializable

// @Serializable属性を付与
@Serializable    
data class UserInfo(
    var userName: String,
    var phoneNumber: String,
    var email: String,
)   

Jsonへ変換

Encode.kt

import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

class Encode {
    fun hoge() {
        val userInfo = UserInfo("Name", "09012345678", "hogehoge@hoge.com")

        val jsonStr = Json.encodeToString(userInfo)
    }
}

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?