如何使用 Kotlin 在 Android 上解析 JSON?
本示例演示如何使用 Kotlin 在 Android 上解析 JSON
步驟 1 − 在 Android Studio 中建立一個新專案,進入 File ⇒ New Project 並填寫所有必需的詳細資訊以建立一個新專案。
步驟 2 − 將以下程式碼新增到 res/layout/activity_main.xml 中。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="70dp" android:background="#008080" android:padding="5dp" android:text="TutorialsPoint" android:textColor="#fff" android:textSize="24sp" android:textStyle="bold" /> <TextView android:textColor="@android:color/background_dark" android:id="@+id/textViewName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="16sp" android:textStyle="bold|italic" /> <TextView android:id="@+id/textViewSal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textViewName" android:layout_centerInParent="true" android:textColor="@android:color/background_dark" android:textSize="16sp" android:textStyle="bold" /> </RelativeLayout>
步驟 3 − 將以下程式碼新增到 src/MainActivity.kt 中
import android.os.Bundle import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import org.json.JSONException import org.json.JSONObject class MainActivity : AppCompatActivity() { lateinit var textViewName: TextView lateinit var textViewSal: TextView var jsonString = "{"employee":{"name":"tutorialspoint","salary":65000}}" lateinit var name:String lateinit var salary:String override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" textViewName = findViewById(R.id.textViewName) textViewSal = findViewById(R.id.textViewSal) try { // get JSONObject from JSON file val obj = JSONObject(jsonString) // fetch JSONObject named employee val employee: JSONObject = obj.getJSONObject("employee") // get employee name and salary name = employee.getString("name") salary = employee.getString("salary") // set employee name and salary in TextView's textViewName.text = "Name: $name" textViewSal.text = "Salary: $salary" } catch (e: JSONException) { e.printStackTrace() } } }
步驟 4 − 將以下程式碼新增到 androidManifest.xml 中
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.q11"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
讓我們嘗試執行你的應用程式。我假設你已將實際的 Android 移動裝置連線到計算機。要從 Android Studio 執行應用,開啟一個專案的活動檔案,並點選工具欄中的執行圖示 。將移動裝置選作選項,然後檢查顯示預設介面的移動裝置。
廣告