Android Json解析与总结
一、JSON定義
JSON(JavaScript Object Notation) 是一種輕量級(jí)的數(shù)據(jù)交換格式。 易于人閱讀和編寫(xiě)。同時(shí)也易于機(jī)器解析和生成。 它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一個(gè)子集。 JSON采用完全獨(dú)立于語(yǔ)言的文本格式,但是也使用了類(lèi)似于C語(yǔ)言家族的習(xí)慣(包括C, C++, C#, Java, JavaScript, Perl, Python等)。 這些特性使JSON成為理想的數(shù)據(jù)交換語(yǔ)言。
二、JSON格式
JSON建構(gòu)于兩種結(jié)構(gòu):
“名稱(chēng)/值”對(duì)的集合(A collection of name/value pairs)。不同的語(yǔ)言中,它被理解為對(duì)象(object),紀(jì)錄(record),結(jié)構(gòu)(struct),字典(dictionary),哈希表(hash table),有鍵列表(keyed list),或者關(guān)聯(lián)數(shù)組 (associative array)。
值的有序列表(An ordered list of values)。在大部分語(yǔ)言中,它被理解為數(shù)組(array)。
JSON具有以下這些形式:
對(duì)象是一個(gè)無(wú)序的“‘名稱(chēng)/值’對(duì)”集合。一個(gè)對(duì)象以“{”(左括號(hào))開(kāi)始,“}”(右括號(hào))結(jié)束。每個(gè)“名稱(chēng)”后跟一個(gè)“:”(冒號(hào));“‘名稱(chēng)/值’ 對(duì)”之間使用“,”(逗號(hào))分隔。
數(shù)組是值(value)的有序集合。一個(gè)數(shù)組以“[”(左中括號(hào))開(kāi)始,“]”(右中括號(hào))結(jié)束。值之間使用“,”(逗號(hào))分隔。
值(value)可以是雙引號(hào)括起來(lái)的字符串(string)、數(shù)值(number)、true、false、 null、對(duì)象(object)或者數(shù)組(array)。這些結(jié)構(gòu)可以嵌套。<"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="/">
以上內(nèi)容摘自:《Json官網(wǎng)》
三、JSON解析常用類(lèi)
1. Android JSON所有相關(guān)類(lèi),都在org.json包下
JSONObject、JSONArray、JSONException、JSONStringer、JSONTokener
2. 常見(jiàn)方法
使用get方法與使用opt方法的區(qū)別?
JsonObject 方法,opt* 與 get* 建議使用opt方法,因?yàn)間et方法如果其內(nèi)容為空會(huì)直接拋出異常。不過(guò)JsonArray.opt*(index)會(huì)有越界問(wèn)題需要特別注意。
opt、optBoolean、optDouble、optInt、optLong、optString、optJSONArray、optJSONObject get、getBoolean、getDouble、getInt、getLong、getString、getJSONArray、getJSONObject?
3.?Android創(chuàng)建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();}?
輸出結(jié)果:
{"intKey":123, "doubleKey":10.1, "longKey":666666666, "stringKey":"lalala", "booleanKey":true, "arrayKey":[111,"second"], "innerObjectKey":{"innerStr":"inner"}}
4. 解析上面創(chuàng)建的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);}?
輸出結(jié)果:
intValue = 123 , doubleValue = 10.1 , longValue = 666666666 , strValue = lalala , booleanValue = true , arrIntValue = 111 , arrStrValue = second , innerStr = inner
?
更多具體信息詳見(jiàn):
《android json解析及簡(jiǎn)單例子》
《Android學(xué)習(xí)筆記44:JSON數(shù)據(jù)解析》
四、Android JSON解析庫(kù)
上面介紹都是使用Android提供的原生類(lèi)解析JSON,最大的好處是項(xiàng)目不需要引入第三方庫(kù),但是如果比較注重開(kāi)發(fā)效率而且不在意應(yīng)用大小增加幾百K的話(huà),有以下JSON可供選擇:
1. Jackson
2. google-gson
3. Json-lib
《兩款JSON類(lèi)庫(kù)Jackson與JSON-lib的性能對(duì)比(新增第三款測(cè)試)》
五、格式化工具
在日常開(kāi)發(fā)中,如果涉及到與服務(wù)器端調(diào)試協(xié)議時(shí),過(guò)程中難免遇到服務(wù)器端發(fā)送格式或者Android客戶(hù)端解析格式出現(xiàn)問(wèn)題,這時(shí)需要把獲取到的JSON打印出來(lái)進(jìn)行問(wèn)題定位,如果JSON比較短一眼就能看出來(lái),但是如果很長(zhǎng)的話(huà)想查找某一個(gè)字段或者JSON數(shù)組中某一位的值顯得特別困難,想要擺脫這種苦惱也很簡(jiǎn)單,把JSON字符串格式化之后會(huì)發(fā)現(xiàn)苦惱瞬間無(wú)影無(wú)蹤。以下是以我常用的Notepad++進(jìn)行舉例,其他的編輯器也肯定有相應(yīng)的JSON插件,自己可以網(wǎng)上查找一下。
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視圖
轉(zhuǎn)載于:https://www.cnblogs.com/xunbu7/p/6501232.html
總結(jié)
以上是生活随笔為你收集整理的Android Json解析与总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 在IntelliJ IDEA中添加rep
- 下一篇: 用Fiddler在Android上抓HT