封装JSON的数据是在服务器端进行封装了,Android更多的工作是解析(JSON对象/JSON数组),所以Android开发JSON数据的解析非常重要
JSON数据,是存储在文件里面:
/data/data/liudeli.mynetwork01/files/pottingJSON1
{
"name":"李四",
"age":99,
"hobby":"爱好是练习截拳道"
}
/data/data/liudeli.mynetwork01/files/pottingJSON2
{
"student":{
"name":"李四",
"age":99,
"hobby":"爱好是练习截拳道"
}
}
/data/data/liudeli.mynetwork01/files/pottingJSON3
{
"student":{
"name":"李四",
"age":99,
"hobby":"爱好是练习截拳道",
"dog":{
"name":"阿黄",
"age":77,
"sex":"母"
}
}
}
/data/data/liudeli.mynetwork01/files/pottingJSONArray1
[
{
"name":"君君",
"age":89,
"sex":"男"
},
{
"name":"小君",
"age":99,
"sex":"女"
},
{
"name":"大君",
"age":88,
"sex":"男"
}
]
/data/data/liudeli.mynetwork01/files/pottingJSONArray2
{
"person":[
{
"name":"君君",
"age":89,
"sex":"男"
},
{
"name":"小君",
"age":99,
"sex":"女"
},
{
"name":"大君",
"age":88,
"sex":"男"
}
]
}
为什么要使用jsonObject.optString, 不使用jsonObject.getString
因为jsonObject.optString获取null不会报错
看着JSON数据,一步一步的解析就好了,当明白JSON数据格式后,解析是非常容易的:
AnalyzeJSONActivity.java
package liudeli.mynetwork01;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.InputStream;
public class AnalyzeJSONActivity extends Activity {
private final String TAG = AnalyzeJSONActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_analyze_json);
}
/**
* 解析JSON对象
* {
* "name":"李四",
* "age":99,
* "hobby":"爱好是练习截拳道"
* }
* @param view
*/
public void analyzeJSON1(View view) {
String result = readFile("pottingJSON1");
// Log.d(TAG, "result:" + result);
try{
JSONObject jsonObject = new JSONObject(result);
/**
* 为什么要使用jsonObject.optString, 不使用jsonObject.getString
* 因为jsonObject.optString获取null不会报错
*/
String name = jsonObject.optString("name", null);
int age = jsonObject.optInt("age", 0);
String hobby = jsonObject.optString("hobby", null);
// 日志打印结果:
Log.d(TAG, "analyzeJSON1解析的结果:name:" + name + " age:" + age + " hobby:" + hobby);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解析JSON对象-带Key
* {
* "student":{
* "name":"李四",
* "age":99,
* "hobby":"爱好是练习截拳道"
* }
* }
* @param view
*/
public void analyzeJSON2(View view) {
String result = readFile("pottingJSON2");
// Log.d(TAG, "result:" + result);
try{
// 整个最大的JSON对象
JSONObject jsonObjectALL = new JSONObject(result);
/**
* 为什么要使用jsonObject.optString, 不使用jsonObject.getString
* 因为jsonObject.optString获取null不会报错
*/
String student = jsonObjectALL.optString("student", null);
if (!TextUtils.isEmpty(student)) {
JSONObject jsonObject = new JSONObject(student);
String name = jsonObject.optString("name", null);
int age = jsonObject.optInt("age", 0);
String hobby = jsonObject.optString("hobby", null);
// 日志打印结果:
Log.d(TAG, "analyzeJSON2解析的结果:name:" + name + " age:" + age + " hobby:" + hobby);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解析JSON对象-嵌套对象
* {
* "student":{
* "name":"李四",
* "age":99,
* "hobby":"爱好是练习截拳道",
* "dog":{
* "name":"阿黄",
* "age":77,
* "sex":"母"
* }
* }
* }
* @param view
*/
public void analyzeJSON3(View view) {
String result = readFile("pottingJSON3");
// Log.d(TAG, "result:" + result);
try{
// 整个最大的JSON对象
JSONObject jsonObjectALL = new JSONObject(result);
/**
* 为什么要使用jsonObject.optString, 不使用jsonObject.getString
* 因为jsonObject.optString获取null不会报错
*/
String student = jsonObjectALL.optString("student", null);
if (!TextUtils.isEmpty(student)) {
JSONObject jsonObject = new JSONObject(student);
String name = jsonObject.optString("name", null);
int age = jsonObject.optInt("age", 0);
String hobby = jsonObject.optString("hobby", null);
// 以下是dog JSON 对象相关的解析
String dogStr = jsonObject.optString("dog", null);
// 定义dog的JSON对象
JSONObject dogJSONObject = new JSONObject(dogStr);
String dogName = dogJSONObject.optString("name", null);
int dogAge = dogJSONObject.optInt("age", 0);
String dogSex = dogJSONObject.optString("sex", null);
// 日志打印结果:
Log.d(TAG, "analyzeJSON3解析的结果:name:" + name + " age:" + age + " hobby:" + hobby + "\n"
+ "dogName:" + dogName + " dogAge:" + dogAge + " dogSex:" + dogSex);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解析JSON数组
* [
* {
* "name":"君君",
* "age":89,
* "sex":"男"
* },
* {
* "name":"小君",
* "age":99,
* "sex":"女"
* },
* {
* "name":"大君",
* "age":88,
* "sex":"男"
* }
* ]
* @param view
*/
public void analyzeJSONArray1(View view) {
String result = readFile("pottingJSONArray1");
// Log.d(TAG, "result:" + result);
try{
// 整个最大的JSON数组
JSONArray jsonArray = new JSONArray(result);
Log.d(TAG, "analyzeJSONArray1 jsonArray:" + jsonArray);
// [{"name":"君君","age":89,"sex":"男"},{"name":"小君","age":99,"sex":"女"},{"name":"大君","age":88,"sex":"男"}]
for (int i = 0; i < jsonArray.length(); i++) {
// JSON数组里面的具体-JSON对象
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.optString("name", null);
int age = jsonObject.optInt("age", 0);
String sex = jsonObject.optString("sex", null);
// 日志打印结果:
Log.d(TAG, "analyzeJSONArray1 解析的结果:name" + name + " age:" + age + " sex:" + sex);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解析JSON数组-带Key
* {
* "person":[
* {
* "name":"君君",
* "age":89,
* "sex":"男"
* },
* {
* "name":"小君",
* "age":99,
* "sex":"女"
* },
* {
* "name":"大君",
* "age":88,
* "sex":"男"
* }
* ]
* }
* @param view
*/
public void analyzeJSONArray2(View view) {
String result = readFile("pottingJSONArray2");
// Log.d(TAG, "result:" + result);
try{
/**
* JSON数组在牛逼,一旦有了 key person 这样的标记,就必须先是个 JSON对象
* 最外层的JSON对象,最大的哪个 { ... }
*/
JSONObject jsonObjectALL = new JSONObject(result);
// 通过标识(person),获取JSON数组
JSONArray jsonArray = jsonObjectALL.getJSONArray("person");
Log.d(TAG, "analyzeJSONArray1 jsonArray:" + jsonArray);
// [{"name":"君君","age":89,"sex":"男"},{"name":"小君","age":99,"sex":"女"},{"name":"大君","age":88,"sex":"男"}]
for (int i = 0; i < jsonArray.length(); i++) {
// JSON数组里面的具体-JSON对象
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.optString("name", null);
int age = jsonObject.optInt("age", 0);
String sex = jsonObject.optString("sex", null);
// 日志打印结果:
Log.d(TAG, "analyzeJSONArray2 解析的结果:name" + name + " age:" + age + " sex:" + sex);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读取文件里面的字符串
* @param fileName
* @return
*/
private String readFile(String fileName) {
String result = null;
try {
InputStream inputStream = openFileInput(fileName);
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
result = new String(bytes);
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 定义一个Bean
*/
/*class Student {
private String name;
private int age;
private String hobby;
public Student(String name, int age, String hobby) {
this.name = name;
this.age = age;
this.hobby = hobby;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", hobby='" + hobby + '\'' +
'}';
}
}*/
}
activity_analyze_json.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解析JSON对象"
android:onClick="analyzeJSON1"
android:layout_weight="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解析JSON对象-带Key"
android:onClick="analyzeJSON2"
android:layout_weight="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解析JSON对象-嵌套对象"
android:onClick="analyzeJSON3"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解析JSON数组"
android:onClick="analyzeJSONArray1"
android:layout_weight="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解析JSON数组-带Key"
android:onClick="analyzeJSONArray2"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
所有解析JSON的Log打印:
analyzeJSON1
12-23 21:46:44.127 8204-8204/liudeli.mynetwork01 D/AnalyzeJSONActivity: analyzeJSON1解析的结果:name:李四 age:99 hobby:爱好是练习截拳道
analyzeJSON2
12-23 21:46:59.161 8204-8204/liudeli.mynetwork01 D/AnalyzeJSONActivity: analyzeJSON2解析的结果:name:李四 age:99 hobby:爱好是练习截拳道
analyzeJSON3
12-23 21:47:12.240 8204-8204/liudeli.mynetwork01 D/AnalyzeJSONActivity: analyzeJSON3解析的结果:name:李四 age:99 hobby:爱好是练习截拳道
dogName:阿黄 dogAge:77 dogSex:母
analyzeJSONArray1
12-23 21:47:35.108 8204-8204/liudeli.mynetwork01 D/AnalyzeJSONActivity: analyzeJSONArray1 解析的结果:name君君 age:89 sex:男
12-23 21:47:35.108 8204-8204/liudeli.mynetwork01 D/AnalyzeJSONActivity: analyzeJSONArray1 解析的结果:name小君 age:99 sex:女
12-23 21:47:35.108 8204-8204/liudeli.mynetwork01 D/AnalyzeJSONActivity: analyzeJSONArray1 解析的结果:name大君 age:88 sex:男
analyzeJSONArray2
12-23 21:47:55.457 8204-8204/liudeli.mynetwork01 D/AnalyzeJSONActivity: analyzeJSONArray2 解析的结果:name君君 age:89 sex:男
12-23 21:47:55.457 8204-8204/liudeli.mynetwork01 D/AnalyzeJSONActivity: analyzeJSONArray2 解析的结果:name小君 age:99 sex:女
12-23 21:47:55.457 8204-8204/liudeli.mynetwork01 D/AnalyzeJSONActivity: analyzeJSONArray2 解析的结果:name大君 age:88 sex:男