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

AndroidでJsonを扱うための設定など

  • プロジェクト
build.gradle.kt
plugins {
    id("org.jetbrains.kotlin.plugin.serialization") version("2.0.20")
}
  • モジュール
build.gradle.kt
plugins {
    id("org.jetbrains.kotlin.plugin.serialization")
}

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.2")
}
  • JSONに対応したデータクラス
JsonData.kt
import kotlinx.serialization.Serializable

//Serializableアノテーション必要
@Serializable
data class JsonData(
    val id:String = "",
    val name:String = ""
)
  • JSON→データ
Sample(抜粋).kt
  //デコード
  val model:JsonData = Json.decodeFromString<JsonData>(jsonStr)

  val data : JsonData = JsonData()
  data.id = "0001"
  data.name = "テスト"
  //エンコード
  val jsonStr : String = Json.encodeToString(data)
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?