Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีใช้ JSONObject แยกวิเคราะห์ JSON ใน Android

ภาพรวม

ตัวอย่างนี้สาธิตวิธีใช้ JSONObject ในการแยกวิเคราะห์ (parse) ข้อมูล JSON และแสดงผลใน TextView

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio → File → New Project และกรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: เลย์เอาต์ (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textStyle="bold"
        android:textSize="24sp"
        android:layout_centerInParent="true" />

</RelativeLayout>

ขั้นตอนที่ 3: โค้ดหลัก (src/MainActivity.java)

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
    public static final String JSON_STRING = 
        "{\"Employee\":{\"Name\":\"Niyaz\",\"Salary\":56000}}";
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);

        try {
            // ดึง JSONObject ชื่อ Employee
            JSONObject emp = (new JSONObject(JSON_STRING)).getJSONObject("Employee");
            
            // อ่านค่าต่างๆ
            String empName = emp.getString("Name");
            int empSalary = emp.getInt("Salary");
            
            // แสดงผล
            String result = "Employee Name: " + empName + "\n" +
                           "Employee Salary: " + empSalary;
            textView.setText(result);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

ขั้นตอนที่ 4: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">

    <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>

การทดสอบ

รันแอปพลิเคชัน จะเห็นข้อมูล JSON ถูกแยกวิเคราะห์และแสดงใน TextView

วิธีใช้ JSONObject แยกวิเคราะห์ JSON ใน Android

คำอธิบายเพิ่มเติม

  • getJSONObject() - ดึงออบเจ็กต์ JSON ย่อย
  • getString() - ดึงค่าสตริง
  • getInt() - ดึงค่าจำนวนเต็ม
  • getBoolean() - ดึงค่าบูลีน
  • getDouble() - ดึงค่าทศนิยม

สำหรับ JSON Array ให้ใช้ getJSONArray() และวนลูปอ่านค่าแต่ละรายการ