文章目錄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源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。