日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android Json解析与总结

發布時間:2024/9/5 Android 65 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android Json解析与总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、JSON定義

JSON(JavaScript Object Notation) 是一種輕量級的數據交換格式。 易于人閱讀和編寫。同時也易于機器解析和生成。 它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一個子集。 JSON采用完全獨立于語言的文本格式,但是也使用了類似于C語言家族的習慣(包括C, C++, C#, Java, JavaScript, Perl, Python等)。 這些特性使JSON成為理想的數據交換語言。

二、JSON格式

JSON建構于兩種結構:
“名稱/值”對的集合(A collection of name/value pairs)。不同的語言中,它被理解為對象(object),紀錄(record),結構(struct),字典(dictionary),哈希表(hash table),有鍵列表(keyed list),或者關聯數組 (associative array)。
值的有序列表(An ordered list of values)。在大部分語言中,它被理解為數組(array)。

JSON具有以下這些形式:

對象是一個無序的“‘名稱/值’對”集合。一個對象以“{”(左括號)開始,“}”(右括號)結束。每個“名稱”后跟一個“:”(冒號);“‘名稱/值’ 對”之間使用“,”(逗號)分隔。

數組是值(value)的有序集合。一個數組以“[”(左中括號)開始,“]”(右中括號)結束。值之間使用“,”(逗號)分隔。

值(value)可以是雙引號括起來的字符串(string)、數值(number)、true、false、 null、對象(object)或者數組(array)。這些結構可以嵌套。<"http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGltZyBzcmM9"http://www.2cto.com/uploadfile/Collfiles/20140321/2014032112540745.gif" width="598" height="278" alt="/">

以上內容摘自:《Json官網》

三、JSON解析常用類

1. Android JSON所有相關類,都在org.json包下
JSONObject、JSONArray、JSONException、JSONStringer、JSONTokener

2. 常見方法
使用get方法與使用opt方法的區別?

JsonObject 方法,opt* 與 get* 建議使用opt方法,因為get方法如果其內容為空會直接拋出異常。不過JsonArray.opt*(index)會有越界問題需要特別注意。

opt、optBoolean、optDouble、optInt、optLong、optString、optJSONArray、optJSONObject get、getBoolean、getDouble、getInt、getLong、getString、getJSONArray、getJSONObject

?

3.?Android創建JSON

private String createJson() throws JSONException {JSONObject jsonObject = new JSONObject();jsonObject.put ("intKey" , 123);jsonObject.put ("doubleKey" , 10.1);jsonObject.put ("longKey" , 666666666);jsonObject.put ("stringKey" , "lalala" );jsonObject.put ("booleanKey" , true);JSONArray jsonArray = new JSONArray();jsonArray.put (0, 111);jsonArray.put("second");jsonObject.put ("arrayKey" , jsonArray);JSONObject innerJsonObject = new JSONObject();innerJsonObject.put ("innerStr" , "inner" );jsonObject.put ("innerObjectKey" , innerJsonObject);Log.e("Json" , jsonObject.toString());return jsonObject.toString();}

?

輸出結果:
{"intKey":123, "doubleKey":10.1, "longKey":666666666, "stringKey":"lalala", "booleanKey":true, "arrayKey":[111,"second"], "innerObjectKey":{"innerStr":"inner"}}

4. 解析上面創建的JSON

private void pareJson(String jsonStr) throws JSONException {JSONObject jsonObject = new JSONObject(jsonStr);int intValue = jsonObject.optInt( "intKey");double doubleValue = jsonObject.optDouble( "doubleKey");long longValue = jsonObject.optLong( "longKey");String strValue = jsonObject.optString( "stringKey");boolean boolValue = jsonObject.optBoolean( "booleanKey");JSONArray array = jsonObject.optJSONArray( "arrayKey");int arrIntValue = array.optInt(0);String arrStrValue = array.optString(1);JSONObject innerJson = jsonObject.optJSONObject("innerObjectKey" );String innerStr = innerJson.optString( "innerStr");Log.e("Json" , "intValue = " + intValue + " , doubleValue = " + doubleValue+ " , longValue = " + longValue + " , strValue = " + strValue+ " , booleanValue = " + boolValue + " , arrIntValue = " + arrIntValue+ " , arrStrValue = " + arrStrValue + " , innerStr = " + innerStr);}

?

輸出結果:
intValue = 123 , doubleValue = 10.1 , longValue = 666666666 , strValue = lalala , booleanValue = true , arrIntValue = 111 , arrStrValue = second , innerStr = inner

?

更多具體信息詳見:
《android json解析及簡單例子》
《Android學習筆記44:JSON數據解析》

四、Android JSON解析庫

上面介紹都是使用Android提供的原生類解析JSON,最大的好處是項目不需要引入第三方庫,但是如果比較注重開發效率而且不在意應用大小增加幾百K的話,有以下JSON可供選擇:

1. Jackson
2. google-gson
3. Json-lib

《兩款JSON類庫Jackson與JSON-lib的性能對比(新增第三款測試)》

五、格式化工具

在日常開發中,如果涉及到與服務器端調試協議時,過程中難免遇到服務器端發送格式或者Android客戶端解析格式出現問題,這時需要把獲取到的JSON打印出來進行問題定位,如果JSON比較短一眼就能看出來,但是如果很長的話想查找某一個字段或者JSON數組中某一位的值顯得特別困難,想要擺脫這種苦惱也很簡單,把JSON字符串格式化之后會發現苦惱瞬間無影無蹤。以下是以我常用的Notepad++進行舉例,其他的編輯器也肯定有相應的JSON插件,自己可以網上查找一下。

Notpad++ Json Viewer插件
安裝:
Notpad++ -> 插件(Plugins) -> Plugin Manager -> Show Plugin Manager -> Avaliable -> 選擇Json View -> 安裝(install)
使用:
選中Json字符串,
插件(Plugins) -> Format Json(快捷鍵Ctrl+Alt+Shift+M)
插件(Plugins) -> Show Json Viewer 顯示Json視圖

轉載于:https://www.cnblogs.com/xunbu7/p/6501232.html

總結

以上是生活随笔為你收集整理的Android Json解析与总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。