android kotlinx.serialization集成

在android kotlin项目中使用kotlinx.serialization进行json解析

kotlinx.serialization github

注意:之前版本的API有改变

pic1.png

pic2.png
  1. projectbuild.gradle
    dependencies 添加 classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
buildscript {
    ext.kotlin_version = "1.4.30"
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
  1. modulebuild.gradle
    plugins 添加 id 'kotlinx-serialization'
    dependencies 添加 implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.1.0"
plugins {
...
    id 'kotlinx-serialization'
}

...

dependencies {
...
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.1.0"
}
  1. 简单使用测试


    test.png

你可能感兴趣的:(android kotlinx.serialization集成)