一.JSON是什么
轻量级的数据交换格式
二.JSON有哪两种数据结构
1.JSONObject 单条JSON数据
2.JSONArray 多条JSON组合
三.如何解析JSONObject(附案例)
简单的JSONObject格式语句
{"name":"张三","age":21,"info":{"class":"三年一班","id":2016001}}
下面是一个简单的JSONObject例子,按下按钮能够获取到JSON语句的信息
布局文件:
<?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="com.example.myapplication.JsonActivity">
<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:textSize="30sp"
android:layout_weight="1"
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:textSize="30sp"
android:layout_weight="1"
android:text="id:"/>
<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:layout_width="match_parent"
android:layout_height="50dp"
android:text="JSON"
android:id="@+id/JSON_btn"/>
</LinearLayout>
效果图:
activity类:
package com.example.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 JsonActivity extends AppCompatActivity {
private TextView name_tv, age_tv, class_tv, id_tv;
private TextView name_tv1, age_tv1, class_tv1, id_tv1;
private Button JSON_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
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.name1_tv);
age_tv1 = findViewById(R.id.age1_tv);
class_tv1 = findViewById(R.id.class1_tv);
id_tv1 = findViewById(R.id.id1_tv);
JSON_btn = findViewById(R.id.JSON_btn);
JSON_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
parseJson1();
}
});
}
private void parseJson1() {
String json_str="{\"name\":\"鹏鹏\",\"age\":20,\"info\":{\"class\":\"安卓1603\",\"id\":163052}}";
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 (附案例)
简单的JSONArray格式语句
[ {"name":"张三","age":21}, {"name":"李四","age":22}]
下面是一个简单的JSONArray 例子,同样是按下按钮能够获取到JSON语句的信息
布局文件:
<?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="com.example.myapplication.JsonActivity">
<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:textSize="30sp"
android:layout_weight="1"
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:textSize="30sp"
android:layout_weight="1"
android:text="id:"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:textSize="30sp"
android:id="@+id/id_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/name1_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/age1_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/class1_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="id:"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:textSize="30sp"
android:id="@+id/id1_tv"
android:layout_weight="1"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="JSON"
android:id="@+id/JSON_btn"/>
</LinearLayout>
效果图:
activity类:
在按钮的点击事件写一个获取JSON语句的方法:
JSON_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
parseJson3();
}
});
parseJson3()方法:
这是一个比较复杂的语句:数组里面还嵌套了单挑语句:
创建一个JSONArray对象,把JSON语句放进去
JSONArray jsonArray= new JSONArray(json_str2);
创建一个JSONObject对象,获取你想要获取第几组的数据游标,这是获取第一组数据,所以游标为0
JSONObject jsonObject=jsonArray.getJSONObject(0);
获取数据通过JSONObject对象,括号里面填JSON语句键值对的键
String name2=jsonObject.getString("name");
把获取到的信息设置到TextView控件上
name_tv.setText(name2);
获取嵌套语句要再创建一个JSONObject对象,通过getJSONObject()获取嵌套语句的键
JSONObject jsonObject1=jsonObject.getJSONObject("info");
[ {"name":"张三","age":21,"info":{"class":"三年一班","id":2016001}}, {"name":"李四","age":22,"info":{"class":"三年二班","id":2016002}}]
下面是方法的完整代码
private void parseJson3() {
String json_str2 = "[{\"name\":\"张三\",\"age\":21,\"info\":{\"class\":\"三年一班\",\"id\":2016001}}, {\"name\":\"李四\",\"age\":22,\"info\":{\"class\":\"三年二班\",\"id\":2016002}}]";
try {
JSONArray jsonArray= new JSONArray(json_str2);
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();
}
}
activity的完整代码:
package com.example.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 JsonActivity extends AppCompatActivity {
private TextView name_tv, age_tv, class_tv, id_tv;
private TextView name_tv1, age_tv1, class_tv1, id_tv1;
private Button JSON_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
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.name1_tv);
age_tv1 = findViewById(R.id.age1_tv);
class_tv1 = findViewById(R.id.class1_tv);
id_tv1 = findViewById(R.id.id1_tv);
JSON_btn = findViewById(R.id.JSON_btn);
JSON_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
parseJson3();
}
});
}
private void parseJson3() {
String json_str2 = "[{\"name\":\"张三\",\"age\":21,\"info\":{\"class\":\"三年一班\",\"id\":2016001}}, {\"name\":\"李四\",\"age\":22,\"info\":{\"class\":\"三年二班\",\"id\":2016002}}]";
try {
JSONArray jsonArray= new JSONArray(json_str2);
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();
}
}
}
按下按钮后的效果图:
JSON是一对花括号{}表示
JSON是以键值对形式组成
JSON的键必须包裹一对双引号
多个键值对中间使用逗号分割
JSONObject是JSON基本单元