- 什么是JSON
- JSON有哪两种结构
- 如何解析JSONObject(附案例)
- 如何解析JSONArray(附案例)
一、什么是JSON
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式
客户端-发送请求-服务端接收-返回给客户端数据
二、JSON两种数据结构
1.单条JSON数据,JSONObject
2.多条JSON组合,JSONArray
三、解析JSONObject
package com.example.a17708.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
public class Main5Activity extends AppCompatActivity {
private TextView name_tv, age_tv;
private TextView class_tv,id_tv;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
name_tv = findViewById(R.id.name_tv);
age_tv = findViewById(R.id.age_tv);
class_tv=findViewById(R.id.class_tv);
id_tv=findViewById(R.id.id_tv);
btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
parseJson();
}
});
}
private void parseJson() {
String json_str = "{\"name\":\"张三\",\"age\":21,\"info\":{\"class\":\"三年一班\",\"id\":2016001}}\n";
try {
JSONObject jsonObject = new JSONObject(json_str);
String name = jsonObject.getString("name");
String age = jsonObject.getString("age");
JSONObject jsonObject1=jsonObject.getJSONObject("info");
String class_name=jsonObject1.getString("class");
String id=jsonObject1.getString("id");
name_tv.setText(name);
age_tv.setText(age);
class_tv.setText(class_name);
id_tv.setText(id);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
四、解析JSONArray
<?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"
tools:context=".Main5Activity">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="50dp">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:text="姓名:"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:textSize="30sp"
android:id="@+id/name_tv"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="50dp">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:textSize="30sp"
android:layout_weight="1"
android:text="年龄:"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:textSize="30sp"
android:id="@+id/age_tv"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="50dp">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:text="班级:"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:textSize="30sp"
android:id="@+id/class_tv"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="50dp">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="30sp"
android:text="学号:"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:textSize="30sp"
android:id="@+id/id_tv"
android:layout_weight="1"/>
</LinearLayout>
<Button
android:id="@+id/btn"
android:text="登录"
android:layout_width="match_parent"
android:layout_height="50dp" />
</LinearLayout>
package com.example.a17708.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Main5Activity extends AppCompatActivity {
private TextView name_tv, age_tv;
private TextView class_tv,id_tv;
private TextView name_tv1,age_tv1,class_tv1,id_tv1;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
name_tv = findViewById(R.id.name_tv);
age_tv = findViewById(R.id.age_tv);
class_tv=findViewById(R.id.class_tv);
id_tv=findViewById(R.id.id_tv);
name_tv1 = findViewById(R.id.name_tv1);
age_tv1 = findViewById(R.id.age_tv1);
class_tv1=findViewById(R.id.class_tv1);
id_tv1=findViewById(R.id.id_tv1);
btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
parseJson();
}
});
}
private void parseJson() {
String json_str = "[ {\"name\":\"张三\",\"age\":21,\"info\":{\"class\":\"三年一班\",\"id\":2016001}}, {\"name\":\"李四\",\"age\":22,\"info\":{\"class\":\"三年二班\",\"id\":2016002}}]\n";
try {
JSONArray jsonArray= new JSONArray(json_str);
JSONObject jsonObject=jsonArray.getJSONObject(0);
String name2=jsonObject.getString("name");
String age2=jsonObject.getString("age");
JSONObject jsonObject1=jsonObject.getJSONObject("info");
String class_name2=jsonObject1.getString("class");
String id2=jsonObject1.getString("id");
name_tv.setText(name2);
age_tv.setText(age2);
class_tv.setText(class_name2);
id_tv.setText(id2);
JSONObject jsonObject2=jsonArray.getJSONObject(1);
String name3=jsonObject2.getString("name");
String age3=jsonObject2.getString("age");
JSONObject jsonObject3=jsonObject2.getJSONObject("info");
String class_name3=jsonObject3.getString("class");
String id3=jsonObject3.getString("id");
name_tv1.setText(name3);
age_tv1.setText(age3);
class_tv1.setText(class_name3);
id_tv1.setText(id3);
} catch (JSONException e) {
e.printStackTrace();
}
}
}