本文讲解android解析json数据。客户端接收服务端发来的4种json数据,分别解析成四种类型Person , List<Person> , List<String> , List<Map<String,Object>>,从MainActivity传递到OtherActivity.
接收的服务器端的4种json数据分别为:
1. {"person":{"address":"香港","id":1001,"name":"刘德华"}}
2. {"persons":[{"address":"香港","id":1001,"name":"刘德华"},{"address":"韩国","id":1001,"name":"宋慧乔"}]}
3. {"listString":["美国","中国","俄罗斯"]}
4. {"listMap":[{"id":1001,"address":"香港","name":"刘德华"},{"id":1002,"address":"韩国","name":"宋慧乔"}]}
客户端相应4种操作 :
1. MainActivity 将json数据解析成Person类,用Intent将Peson对象传递给OtherActvity. 设置标记click_flag = person
2. MainActivity 将json数据解析成List<Person>,用Intent将List<Person>传递给OtherActvity. 设置标记click_flag = persons
3. MainActivity 将json数据解析成List<String>,用Intent将List<String>传递给OtherActvity. 设置标记click_flag = listString
4. MainActivity 将json数据解析成List<Map<String,Object>>,用Intent将List<Map<String,Object>>传递给OtherActvity. 设置标记click_flag = listMap
----------------------------------------------------------------------------------------------------------------------------------
三点重点注意点:
1. 在AndroidManifest.xml中 加上
<!-- 添加访问网络的授权 -->
<uses-permission android:name="android.permission.INTERNET" />
2. android中Http访问时 connection.getResponseCode()不被执行 。添加访问网络的授权后,仍不能执行
3. 在android的两个activity中,如何用Intent传递Person对象,List<Person>,List<Map<String,Object>>类型。
一.Intent 传递 Person对象
传递 intent.putExtra("person", (Serializable)person);
接收 Person person =(Person) intent.getSerializableExtra("person");
二.Intent 传递 List<Person>对象
传递 intent.putExtra("persons", (Serializable) list);
接收 List<Person> list = (List<Person>) intent.getSerializableExtra("persons");
三. Intent传递 List<Map<String,Object>>类型
传递 intent.putExtra("listMap", (Serializable) list);
接收 List<Map<String, Object>> list = (List<Map<String, Object>>) intent.getSerializableExtra("listMap");
注意:Intent 传递的对象object必须都实现Serializable接口,List<Object>内的object也必须要实现Serializable接口.
-------------------------------------------------------------------------------------------------------------------------------
1.点击“解析person数据”,反馈结果如下:
2. 点击“解析persons数据”,反馈结果如下:
3. 点击“解析listString数据”,反馈结果如下:
4.点击“解析listMap数据”,反馈结果如下:
----------------------------------------------------------------------------------------------------------------------
程序思路:
1.服务器端产生4种类型的json数据,客户端android用GET方式分别获取服务器端返回的json数据,http 请求传递参数action_flag. 服务器端根据action_flag 返回不同的json数据给客户端。
2.客户端将4种不同的json数据解析成4中不同的结果类型(Person,List<Person>,List<String>,List<Map<String,Object>>),用Intent方式由MainActivity传递给OtherActivity,在OtherActvity中显示解析的结果。
----------------------------------------------------------------------------------------------------------------------
客户端android工程目录(左)和服务器端web工程目录(右)
1. AndroidManifest.xml 加上网络访问权限
<!-- 添加访问网络的授权 -->
<uses-permission android:name="android.permission.INTERNET" />
2.activity_main.xml 主机面
<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">"
<Button
android:id="@+id/person"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="解析Person数据" />
<Button
android:id="@+id/persons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="解析Persons数据" />
<Button
android:id="@+id/listString"
android:layout_width="match_parent"
android:layout_height="wr