JSON是近幾年才流行的一種新的資料格式,它與XML非常相似,都是用來存儲資料的。但JSON相對于XML來說,解析速度更快,占用空間更小。
下面我們直接來幹貨。
1、設計布局檔案activity_json.xml
布局屬性如下:

布局檔案activity_json.xml源代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_marginLeft="20dp"
android:layout_marginTop="2dp"
android:text="城市"
android:textSize="24sp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:inputType="textPersonName"
android:textSize="24sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:layout_marginLeft="20dp"
android:layout_marginTop="2dp"
android:text="氣溫"
android:textSize="24sp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:inputType="textPersonName"
android:textSize="24sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_marginTop="2dp"
android:layout_marginLeft="20dp"
android:text="解析單個jason資料"
android:textSize="24sp"/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView"
app:srcCompat="@drawable/logo"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txt3"
android:textSize="24sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:layout_marginTop="2dp"
android:layout_marginLeft="20dp"
android:text="解析單多個jason資料"
android:textSize="24sp"/>
</LinearLayout>
</LinearLayout>
2、設計控制檔案JsonActivity.java
(初始化資料,并添加事件處理)
運作效果如下
控制檔案JsonActivity.java源代碼
package com.example.myapplication5;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonActivity<TSONObject> extends AppCompatActivity implements View.OnClickListener {
EditText txt1,txt2;
TextView txt3;
JSONObject p1,p2,p3;
JSONArray weather;
Button jsonBtn,arrayBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
txt1 = (EditText)findViewById(R.id.editText);
txt2 = (EditText)findViewById(R.id.editText2);
txt3 = (TextView)findViewById(R.id.txt3);
jsonBtn = (Button) findViewById(R.id.button);
jsonBtn.setOnClickListener(this);
arrayBtn = (Button) findViewById(R.id.button2);
arrayBtn.setOnClickListener(this);
try {
p1=new JSONObject("{\"城市\":\"大理\", \"氣溫\":\"0-14度,多雲\"}");
p2=new JSONObject("{\"城市\":\"成都\", \"氣溫\":\"2-4度,小雨\"}");
p3=new JSONObject("{\"城市\":\"拉薩\", \"氣溫\":\"-9-5度,多雲\"}");
weather =new JSONArray();
weather.put(p1);
weather.put(p2);
weather.put(p3);
} catch ( JSONException e) {
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
if(v == jsonBtn)
setJsonData();
else if(v == arrayBtn)
setandgetArrayData();
}
void setJsonData() {
try {
JSONObject test= new JSONObject();
test.put("城市","深圳");
test.put("氣溫",30);
String jc=test.getString("城市");
txt1.setText(jc);
int jw = test.getInt("氣溫");
txt2.setText(Integer.toString(jw));
}catch (JSONException e){ }
}
void setandgetArrayData() {
try {
String jc,jw;
int length = weather.length();
for(int i=0; i<length; i++){ //周遊JSONArray
JSONObject jsonObject = weather.getJSONObject(i);
jc = jsonObject.getString("城市") + ":";
jw = jsonObject.getString("氣溫") + "\n";
txt3.append(jc + jw);
}
}catch (JSONException e){ }
}
}