如何在 Android 中使用 Kotlin 迭代 JSON 陣列?


此示例演示如何在 Android 中使用 Kotlin 迭代 JSON 陣列。

步驟 1 − 在 Android Studio 中新建一個專案,轉到“檔案”→“新建專案”,並填寫所有必需資訊以新建一個專案。

步驟 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"
   android:padding="8dp"
   tools:context=".MainActivity">
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="50dp"
      android:text="Tutorials Point"
      android:textAlignment="center"
      android:textColor="@android:color/holo_green_dark"
      android:textSize="32sp"
      android:textStyle="bold" />
   <TextView
      android:id="@+id/textView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      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.JSONObject
@Suppress("NAME_SHADOWING")
class MainActivity : AppCompatActivity() {
   private lateinit var textView: TextView
   var strJson = ("{ \"Employee\" :[{\"ID\":\"01\",\"Name\":\"Sam\",\"Salary\":\"50000\"},"
+ "{\"ID\":\"02\",\"Name\":\"Shankar\",\"Salary\":\"60000\"}] }")
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      title = "KotlinApp"
      textView = findViewById(R.id.textView)
      val data = StringBuilder()
      val jsonObject = JSONObject(strJson)
      val jsonArray = jsonObject.optJSONArray("Employee")
      for (i in 0 until jsonArray.length()) {
         val jsonObject = jsonArray.getJSONObject(i)
         val id = jsonObject.optString("ID").toInt()
         val name = jsonObject.optString("Name")
         val salary = jsonObject.optString("Salary").toFloat()
         data.append("Employee ").append(i).append(" : 
ID= ") .append(id).append("
" + "Name= ") .append(name).append("
Salary= ") .append(salary).append("

")       }       textView.text = data.toString()    } }

步驟 4 − 將以下程式碼新增到 androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="app.com.kotlipapp">
   <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 Studio 執行應用,請開啟一個專案活動檔案並單擊工具欄上的執行  圖示。選擇你的移動裝置作為選項,然後檢視會顯示預設介面的移動裝置 −

單擊 此處 下載專案程式碼。

更新於: 2020 年 4 月 21 日

3K+ 瀏覽次數

開啟您的 職業生涯

完成課程,獲得認證

入門
廣告
© . All rights reserved.