1、什么是JSON?
JSON(JavvaScript Object Notation)是一种轻量级的数据交互格式,JSON应用非常广泛,在APP开发中大多数服务端返回的接口数据都是采用JSON数据格式的。
2、JSON的两种数据结构
单挑JSON数据,Android中称之为JSONObject
多条JSON数据组合,Android中称之为JSONArray
注意:
JSON是一对大括号{ }表示;
JSON是以键值对形式组成;
JSON的键必须使用一对双引号包裹;
多个键值对之间需使用逗号分割;
重点:
JSON的键必须是字符串,并且需要用一对双引号包裹;
JSON的值可以是int、long、double、boolean、String、JSON类型的;
单条JSON数据解析方法
getString(String name):通过建名称,得到对应的字符串;
getLong(String name):通过建名称,得到对应的长整形;
getInt(String name):通过建名称,得到对应的整形;
getDouble(String name):通过建名称,得到对应的浮点数;
getBoolean(String name):通过建名称,得到对应的布尔值;
getJSONObject(String name):通过建名称,得到对应的JSON;
getJSONArray(String name):通过建名称,得到对应的JSON数组;
3、如何解析JSONObject
案例:
在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"
tools:context=".JsonActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/name_tv"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/age_tv"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/class_tv"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/id_tv"
/>
<Button
android:id="@+id/json_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="解析Json"/>
</LinearLayout>
Activity:
package com.example.jsonapplication;
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 JsonActivity extends AppCompatActivity implements View.OnClickListener{
private TextView name_tv;
private TextView age_tv;
private TextView class_tv;
private TextView id_tv;
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);
json_btn = findViewById(R.id.json_btn);//绑定ID
json_btn.setOnClickListener(this);//点击按钮解析
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.json_btn:
parseJson();//将解析JSON数据放在这个方法里
break;
default:
break;
}
}
private void parseJson() {
String json_str = "{\"name\":\"张三\",\"age\":21,\"info\":{\"class\":\"三年一班\",\"id\":2016001}}\n";//JSON数据
try {
JSONObject mjsonObject = new JSONObject(json_str);//创建JSONObject对象开始解析
String name = mjsonObject.getString("name");//解析name
int age = mjsonObject.getInt("age");//解析age
JSONObject Jsonbject = mjsonObject.getJSONObject("info");//这组JSON数据是嵌套数据,需要再次进入info中解析class和id,可以理解为拆快递手机,拆完第一层保证还要再进入下一次拆开手机盒子。
String classname = Jsonbject.getString("class");//解析class
int id = Jsonbject.getInt("id");//解析id
name_tv.setText(name);
age_tv.setText(age+"");
class_tv.setText(classname);
id_tv.setText(id+"");//将解析出来的值设置为控件内容
} catch (JSONException e) {
e.printStackTrace();
}
}
}
如何解析JSONArray:
照例在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"
tools:context=".JsonArrayActivity">
<TextView
android:id="@+id/name_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/age_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/name1_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/age1_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="解析JsonArray"
android:id="@+id/jsonArray_btn"/>
</LinearLayout>
Activity:
package com.example.jsonapplication;
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 JsonArrayActivity extends AppCompatActivity implements View.OnClickListener{
private TextView name_tv;
private TextView name1_tv;
private TextView age_tv;
private TextView age1_tv;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json_array);
name_tv = findViewById(R.id.name_tv);
name1_tv = findViewById(R.id.name1_tv);
age_tv = findViewById(R.id.age_tv);
age1_tv = findViewById(R.id.age1_tv);
btn = findViewById(R.id.jsonArray_btn);//绑定id
btn.setOnClickListener(this);//按下按钮解析
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.jsonArray_btn:
parseJson();
break;
default:
break;
}
}
private void parseJson() {
String jsArray = "[{\"name\":\"张三\",\"age\":21},{\"name\":\"李四\",\"age\":22}]\n";
try {
JSONArray jsonArray = new JSONArray(jsArray);
//创建JSONArray对象开始解析
JSONObject jsobj1 = jsonArray.getJSONObject(0);
//因为是数组,先要确定游标位置,从0开始解析
String name = jsobj1.getString("name");
//解析name
JSONObject jsobj2 = jsonArray.getJSONObject(0);
int age = jsobj2.getInt("age");
//解析age
JSONObject jsobj3 = jsonArray.getJSONObject(1);
//第二组数据,游标移到1
String name1 = jsobj3.getString("name");
//解析name
JSONObject jsobj4 = jsonArray.getJSONObject(1);
int age1 = jsobj4.getInt("age");
//解析age
name_tv.setText(name);
age_tv.setText(age+"");
name1_tv.setText(name1);
age1_tv.setText(age1+"");
} catch (JSONException e) {
e.printStackTrace();
}
}
}