如何在 Android 上解析 JSON?
此示例演示如何在 android 中解析 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: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/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:text="Name" android:textColor="#000" android:textSize="20sp" /> <TextView android:id="@+id/tvSalary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="80dp" android:text="Salary" android:textColor="#000" android:textSize="20sp" /> </RelativeLayout>
步驟 3 − 將以下程式碼新增到 src/MainActivity.java 中
package com.example.myapplication;
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 {
String JSON_STRING="{\"employee\":{\"name\":\"adolf hitler\",\"salary\":65000}}";
String name, salary;
TextView employeeName, employeeSalary;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the reference of TextView's
employeeName=(TextView) findViewById(R.id.tvName);
employeeSalary=(TextView) findViewById(R.id.tvSalary);
try {
// get JSONObject from JSON file
JSONObject obj=new JSONObject(JSON_STRING);
// fetch JSONObject named employee
JSONObject employee=obj.getJSONObject("employee");
// get employee name and salary
name=employee.getString("name");
salary=employee.getString("salary");
// set employee name and salary in TextView's
employeeName.setText("Name: "+name);
employeeSalary.setText("Salary: "+salary);
} catch (JSONException e) {
e.printStackTrace();
}
}
}步驟 4 − 將以下程式碼新增到 manifests/AndroidManifest.xml 中
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication"> <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 執行應用程式,請開啟您的一個專案的活動檔案,然後單擊執行
工具欄中的圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 −

單擊 此處 下載專案程式碼
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP