文章目录html
1、运行效果java
2、实现步骤android
一、建立安卓应用ParseJsonapache
二、切换到Project视图,在main目录下建立assets目录编程
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于ECMAScript( (欧洲计算机协会制定的JavaScript规范)的一个子集,采用彻底独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成,并有效地提高网络传输效率。
1、运行效果
先单击【解析JSON】按钮,会弹出吐司提示用户先读取Json文件:
单击【读取JSON】按钮:
单击【解析JSON】按钮:
2、实现步骤
一、建立安卓应用ParseJson
二、切换到Project视图,在main目录下建立assets目录
将项目切换回Android视图:
三、在assets目录下建立test.json文件
[ { "id": 1, "name": "计算机基础教程", "press": "清华大学出版社", "author": "李晓云", "price": 30.5 }, { "id": 2, "name": "Java程序设计", "press": "水利水电出版社", "author": "张国锋", "price": 40 }, { "id": 3, "name": "安卓应用开发", "press": "北京大学出版社", "author": "郑晓华", "price": 60.5 }, { "id": 4, "name": "PHP应用开发教程", "press": "南京大学出版社", "author": "滕玉国", "price": 27.5 } ]
四、主布局文件activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" > <Button android:id="@+id/btn_read_json" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="doReadJson" android:text="@string/read_json" /> <Button android:id="@+id/btn_parse_json" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="doParseJson" android:text="@string/parse_json" /> </LinearLayout> <ImageView android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:background="#bbbbbb" /> <ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#0000ff" android:textSize="15sp" /> </ScrollView> </LinearLayout>
五、字符串资源文件strings.xml
<resources> <string name="app_name">读取与解析JSON</string> <string name="read_json">读取JSON</string> <string name="parse_json">解析JSON</string> </resources>
六、修改模块的build.gradle文件,添加依赖
七、主界面类MainActivity
package net.hw.parse_json; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.TextView; import android.widget.Toast; import org.apache.http.util.EncodingUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.io.InputStream; /** * Created by howard on 2018/3/14. */ public class MainActivity extends Activity { /** * 文件内容字符串 */ private String content; /** * 文件内容标签 */ private TextView tvContent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 利用布局资源文件设置用户界面 setContentView(R.layout.activity_main); // 经过资源标识得到控件实例 tvContent = findViewById(R.id.tv_content); } /** * 读取Json文件 * * @param view */ public void doReadJson(View view) { try { // 读取assets目录里的test.json文件,获取字节输入流 InputStream is = getResources().getAssets().open("test.json"); // 获取字节输入流长度 int length = is.available(); // 定义字节缓冲区 byte[] buffer = new byte[length]; // 读取字节输入流,存放到字节缓冲区里 is.read(buffer); // 将字节缓冲区里的数据转换成utf-8字符串 content = EncodingUtils.getString(buffer, "utf-8"); // 将字符串显示在标签里 tvContent.setText(content); // 设置标签文本颜色 tvContent.setTextColor(Color.BLUE); // 关闭输入流 is.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 解析Json * * @param view */ public void doParseJson(View view) { // 判断用户是否读取了Json文件 if (content == null) { Toast.makeText(this, "请先读取Json文件!", Toast.LENGTH_SHORT).show(); } else { try { // 清空标签内容 tvContent.setText(""); // 基于content字符串建立Json数组 JSONArray jsonArray = new JSONArray(content); // 遍历Json数组 for (int i = 0; i < jsonArray.length(); i++) { // 经过下标获取json数组元素——Json对象 JSONObject jsonObject = jsonArray.getJSONObject(i); // 对Json对象按键取值 int id = jsonObject.getInt("id"); String name = jsonObject.getString("name"); String press = jsonObject.getString("press"); String author = jsonObject.getString("author"); double price = jsonObject.getDouble("price"); // 拼接成一本图书信息 String book = "编号:" + id + "\n书名:" + name + "\n出版社:" + press + "\n做者:" + author + "\n单价:" + price + "\n\n"; // 将图书信息追加到标签里 tvContent.append(book); } // 设置标签文本颜色 tvContent.setTextColor(Color.RED); } catch (JSONException e) { e.printStackTrace(); } } } }
八、运行程序,查看效果
先单击【解析JSON】按钮,会弹出吐司提示用户先读取Json文件:
单击【读取JSON】按钮:
单击【解析JSON】按钮:
3、课堂练习
一、修改读取JSON按钮的单击事件处理代码,要求采用缓冲输入流BufferReader来读取json文件。
二、编写一个解析Json字符串的安卓程序,界面包含一个编辑框、一个按钮和一个标签,编辑框用于输入Json字符串,单击按钮,将解析的内容显示在标签里。
用于测试的Json字符串1:
{
"id"
:
1
,
"name"
:
"李国强"
,
"gender"
:
"男"
,
"age"
:
30
,
"address"
:{
"country"
:
"中国"
,
"province"
:
"四川"
,
"city"
:
"泸州"
,
"street"
:
"丹霞路"
,
"num"
:
"20号"
}
}
用于测试的Json字符串2:
[
{
"id"
:
1
,
"name"
:
"李国强"
,
"gender"
:
"男"
,
"age"
:
30
,
"address"
:{
"country"
:
"中国"
,
"province"
:
"四川"
,
"city"
:
"泸州"
,
"street"
:
"丹霞路"
,
"num"
:
"20号"
}
},
{
"id"
:
2
,
"name"
:
"张三丰"
,
"gender"
:
"男"
,
"age"
:
40
,
"address"
:{
"country"
:
"中国"
,
"province"
:
"湖南"
,
"city"
:
"长沙"
,
"street"
:
"人民路"
,
"num"
:
"18号"
}
},
{
"id"
:
3
,
"name"
:
"唐晓芙"
,
"gender"
:
"女"
,
"age"
:
20
,
"address"
:{
"country"
:
"中国"
,
"province"
:
"广东"
,
"city"
:
"珠海"
,
"street"
:
"天虹路"
,
"num"
:
"2号"
}
}
]
本文分享 CSDN - howard2005。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。