現在kotlinでjsonを扱いたく勉強中です。
やりたい事は
assetsファイルを作成してその中にjsonファイルを作りました。そのjsonの中の一つの値をAndroid画面に取り出したいと思っています。
コードにエラーは見られないのですが、いざ、アプリを起動すると下記のエラーが出てしまいます。これが解決できません。
どなたかご教授よろしくお願いいたします。
エラー内容
e: /Users/~~~~~/myapplication/MainActivity.kt: (25, 1): Your current kotlinx.serialization core version is too low, while current Kotlin compiler plugin 1.6.21 requires at least 1.0-M1-SNAPSHOT. Please update your kotlinx.serialization runtime dependency.
MainActivity.kt
import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import kotlinx.serialization.Serializable import android.content.res.Resources import android.widget.TextView import kotlinx.serialization.builtins.list import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonConfiguration import java.io.BufferedReader import java.io.InputStreamReader class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val TV = findViewById<TextView>(R.id.TV) val sights = getSights(resources) TV.text = sights[0].kind } } @Serializable class Sight ( val name : String, val imageName : String, val description : String, val kind : String ) fun getSights(resources: Resources): List<Sight> { val assetManager = resources.assets val inputStream = assetManager.open("sights.json") val bufferedReader = BufferedReader(InputStreamReader(inputStream)) val str: String = bufferedReader.readText() val obj = Json(JsonConfiguration.Stable) .parse(Sight.serializer().list, str) return obj }
assets/sights.json
[ { "name": "アンコール・ワット", "imageName": "angkor", "description": "アンコール・ワットは、カンボジア北西部に位置するユネスコの世界遺産であるアンコール遺跡の一つであり、その遺跡群を代表する寺院。ヒンドゥー教寺院として作られたが、16世紀後半に仏教寺院に改修され、現在も上座部仏教寺院となっている。", "kind": "文化遺産" }, { "name": "ホースシューベンド", "imageName": "horseshoebend", "description": "ホースシューベンド(Horseshoe Bend)とは、アメリカ合衆国アリゾナ州ページの町付近にある、コロラド川が蹄鉄(horseshoe)の形に穿入蛇行している場所の名前である。グレンキャニオンダムとパウエル湖から少し下流、ページの南約6キロメートルに位置している。国道89号線から1.2キロメートル歩くとたどり着ける。", "kind": "自然・公園" } ]
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/TV" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
build.gradle(Project:
buildscript { ext.kotlin_version = "1.6.21" repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version" } } plugins { id 'com.android.application' version '7.1.2' apply false id 'com.android.library' version '7.1.2' apply false id 'org.jetbrains.kotlin.android' version '1.6.21' apply false } task clean(type: Delete) { delete rootProject.buildDir }
build.gradle(Module:
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'kotlinx-serialization' } android { compileSdk 32 defaultConfig { applicationId "com.example.myapplication" minSdk 21 targetSdk 32 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } } dependencies { implementation 'androidx.core:core-ktx:1.7.0' implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'com.google.android.material:material:1.6.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.3' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0" }
まだ回答がついていません
会員登録して回答してみよう