android Json详解
Json:一種輕量級的數據交換格式,具有良好的可讀和便于快速編寫的特性。業內主流技術為其提供了完整的解決方案(有點類似于正則表達式?,獲得了當今大部分語言的支持),從而可以在不同平臺間進行數據交換。JSON采用兼容性很高的文本格式,同時也具備類似于C語言體系的行為。?–?Json.org
官網地址:http://www.json.org/
JSON Vs XML
1.JSON和XML的數據可讀性基本相同
2.JSON和XML同樣擁有豐富的解析手段
3.JSON相對于XML來講,數據的體積小
4.JSON與JavaScript的交互更加方便
5.JSON對數據的描述性比XML較差
6.JSON的速度要遠遠快于XML
一、JSON語法
?
? JSON 語法規則
?
- 數據在名稱/值對中
- 數據由逗號分隔
- 花括號保存對象
- 方括號保存數組
?
JSON 名稱/值對
? ?JSON 數據的書寫格式是:名稱/值對。
? ?名稱/值對包括字段名稱(在雙引號中),后面寫一個冒號,然后是值:
"firstName" : "John"?JSON 值
? JSON 值可以是:
- 數字(整數或浮點數)
- 字符串(在雙引號中)
- 邏輯值(true 或 false)
- 數組(在方括號中)
- 對象(在花括號中)
- null
- JSONObject
- JSONArray
JSON 對象
JSON 對象在花括號中書寫:
對象可以包含多個名稱/值對:
{ "firstName":"John" , "lastName":"Doe" } 一個{}就是一個JSONObjectJSON 數組
JSON 數組在方括號中書寫:
數組可包含多個對象:
{ "employees": [ { "firstName":"John" , "lastName":"Doe" }, { "firstName":"Anna" , "lastName":"Smith" }, { "firstName":"Peter" , "lastName":"Jones" } ] }在上面的例子中,對象 "employees" 是包含三個對象的數組。每個對象代表一條關于某人(有姓和名)的記錄。
二、android提供的json解析類?
android的json解析部分都在包org.json下,主要有以下幾個類:?
JSONObject:可以看作是一個json對象,這是系統中有關JSON定義的基本單元,其包含一對兒(Key/Value)數值。它對外部(External: ? 應用toString()方法輸出的數值)調用的響應體現為一個標準的字符串(例如:{"JSON": "Hello, World"},最外被大括號包裹,其中的Key和Value被冒號":"分隔)。其對于內部(Internal)行為的操作格式略微,例如:初始化一個JSONObject實例,引用內部的put()方法添加數值:new JSONObject().put("JSON", "Hello, World!"),在Key和Value之間是以逗號","分隔。Value的類型包括:Boolean、JSONArray、JSONObject、Number、String或者默認值JSONObject.NULLobject 。
JSONStringer:json文本構建類 ,根據官方的解釋,這個類可以幫助快速和便捷的創建JSON text。其最大的優點在于可以減少由于格式的錯誤導致程序異常,引用這個類可以自動嚴格按照JSON語法規則(syntax rules)創建JSON text。每個JSONStringer實體只能對應創建一個JSON text。
JSONArray:它代表一組有序的數值。將其轉換為String輸出(toString)所表現的形式是用方括號包裹,數值以逗號”,”分隔(例如: ? ? [value1,value2,value3],大家可以親自利用簡短的代碼更加直觀的了解其格式)。這個類的內部同樣具有查詢行為, ? ? get()和opt()兩種方法都可以通過index索引返回指定的數值,put()方法用來添加或者替換數值。同樣這個類的value類型可以包括:Boolean、JSONArray、JSONObject、Number、String或者默認值JSONObject.NULL object。
JSONTokener:json解析類?
JSONException:json中用到的異常?
1.JSONObject,JSONArray解析,創建Json
示例代碼:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | /*? ?????json:{? ?????"languages":[? ?????{"id":1,"ide":"Eclispe","name":"java"},? ?????{"id":2,"ide":"Xcode","name":"Swift"},? ?????{"id":3,"ide":"Visual?Studio","name":"C++"}],? ?????"cat":{"cat":"miao"}? ?????}? */?? ????public?void?creatJson2(){?? ??????????? ????????try?{?? ????????????JSONObject?root?=?new?JSONObject();?? ????????????JSONObject?cat?=?new?JSONObject();?? ????????????cat.put("cat",?"miao");?? ????????????JSONArray?array?=?new?JSONArray();?? ??????????????? ????????????JSONObject?lan1?=?new?JSONObject();?? ????????????lan1.put("id",?1).put("ide",?"Eclispe").put("name",?"java");?? ??????????????? ????????????JSONObject?lan2?=?new?JSONObject();?? ????????????lan2.put("id",?2).put("ide",?"Xcode").put("name",?"Swift");?? ????????????JSONObject?lan3?=?new?JSONObject();?? ????????????lan3.put("id",?3).put("ide",?"Visual?Studio").put("name",?"C++");?? ????????????array.put(lan1);?? ????????????array.put(lan2);?? ????????????array.put(lan3);?? ??????????????? ????????????root.put("languages",?array);?? ????????????root.put("cat",?cat);?? ??????????????? ????????????System.out.println("json:"+root.toString());?? ??????????????? ????????}?catch?(JSONException?e)?{?? ????????????//?TODO?Auto-generated?catch?block?? ????????????e.printStackTrace();?? ????????}?? ????} |
然后是解析的代碼:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | public?void?parseJson(){?? ????????try?{?? ????????????InputStreamReader?is?=?new?InputStreamReader(getAssets().open("test2.json"),?"UTF-8");?? ????????????BufferedReader?br?=?new?BufferedReader(is);?? ????????????String?line;?? ????????????StringBuilder?builder?=?new?StringBuilder();?? ????????????while((line=br.readLine())!=null){?? ????????????????builder.append(line);?? ????????????}?? ????????????is.close();br.close();?? ?????????JSONObject?root?=?new?JSONObject(builder.toString());?? ?????????System.out.println("cat:"+root.getString("cat"));?? ?????????JSONArray?array?=?root.getJSONArray("languages");?? ?????????for(int?i=0;i<array.length();i++){?? ?????????????JSONObject?lan?=?array.getJSONObject(i);?? ?????????????System.out.println("..........");?? ?????????????System.out.println("id="+lan.getInt("id"));?? ?????????????System.out.println("ide="+lan.getString("ide"));?? ?????????????System.out.println("name="+lan.getString("name"));?? ?????????}?? ???????????? ????????}?catch?(UnsupportedEncodingException?e)?{?? ????????????//?TODO?Auto-generated?catch?block?? ????????????e.printStackTrace();?? ????????}?catch?(IOException?e)?{?? ????????????//?TODO?Auto-generated?catch?block?? ????????????e.printStackTrace();?? ????????}catch?(JSONException?e)?{?? ????????????//?TODO?Auto-generated?catch?block?? ????????????e.printStackTrace();?? ????????}?? ????} |
?這時解析的源文件:
| 1 2 3 4 5 6 7 8 | {? ???"languages":[? ???{"id":1,"ide":"Eclipse","name":"java"},? ???{"id":2,"ide":"Xcode","name":"Swift"},? ???{"id":3,"ide":"Visual Studio","name":"C++"}? ???],? ???"cat":"miao"? }? |
2.JSONStringer生成json?
Stringers?only encode well-formed JSON strings. In particular:
- The stringer must have exactly one top-level array or object.
- Lexical scopes must be balanced: every call to?array()?must have a matching call to?endArray()?and every call to?object()?must have a matching call to?endObject(). ?//每次調用array(),必須匹配endArray,object,endObject同理。
- Arrays may not contain keys (property names).
- Objects must alternate keys (property names) and values.
- Values are inserted with either literal?value?calls, or by nesting arrays or objects.
它定義的所有方法:
它定義的所有方法:| ? | JSONStringer() |
| ? | JSONStringer?array() Begins encoding a new array. |
| ? | JSONStringer?endArray() Ends encoding the current array. |
| ? | JSONStringer?endObject() Ends encoding the current object. |
| ? | JSONStringer?key(String?name) Encodes the key (property name) to this stringer. |
| ? | JSONStringer?object() Begins encoding a new object. |
| ? | String?toString() Returns the encoded JSON string. |
| ? | JSONStringer?value(double value) Encodes?value?to this stringer. |
| ? | JSONStringer?value(Object?value) Encodes?value. |
| ? | JSONStringer?value(long value) Encodes?value?to this stringer. |
| ? | JSONStringer?value(boolean value) Encodes?value?to this stringer. |
?示例代碼:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | /*json:{? ?????????"languages":[? ?????????{"id":1,"ide":"Eclispe","name":"java"},? ?????????{"id":2,"ide":"Xcode","name":"Swift"},? ?????????{"id":3,"ide":"Visual?Studio","name":"C++"}],? ?????????"cat":{"name":"miao"}? ?????????}*/?? ????public?String?createJson(){?? ????????JSONStringer?stringer?=?new?JSONStringer();?? ????????//every?call?to?array()?must?have?a?matching?call?to?endArray()?and?? ????????//every?call?to?object()?must?have?a?matching?call?to?endObject().?? ????????try?{?? ????????????stringer.object();?? ????????????stringer.key("languages");?? ????????????stringer.array();?? ????????????//數組中的三個對象?? ????????????stringer.object();?? ????????????stringer.key("id").value(1).key("ide").value("Eclispe").key("name").value("java");?? ????????????stringer.endObject();?? ??????????????? ????????????stringer.object();?? ????????????stringer.key("id").value(2).key("ide").value("Xcode").key("name").value("Swift");?? ????????????stringer.endObject();?? ??????????????? ????????????stringer.object();?? ????????????stringer.key("id").value(3).key("ide").value("Visual?Studio").key("name").value("C++");?? ????????????stringer.endObject();?? ????????????stringer.endArray();//數組結束?? ??????????????? ????????????stringer.key("cat");?? ????????????stringer.object();?? ????????????stringer.key("name").value("miao").endObject();?//結束object?? ??????????????? ????????????stringer.endObject();?? ??????????????? ????????????System.out.println("json:"+stringer.toString());?? ??????????????? ????????}?catch?(JSONException?e)?{?? ????????????//?TODO?Auto-generated?catch?block?? ????????????e.printStackTrace();?? ????????}?? ????????return?stringer.toString();?? ????} |
? Json數據的解析與生成還是很簡單的,可以多去官網看看,多看看文檔。。。android總結任重道遠啊,寫博文就是動力啊,堅持堅持。。。。。
另附一篇很好的博文,介紹了很多方法:http://www.open-open.com/lib/view/open1326376799874.html
轉載于:https://www.cnblogs.com/Free-Thinker/p/9708197.html
總結
以上是生活随笔為你收集整理的android Json详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 82 删除排序链表中的
- 下一篇: 控制台打印列未定义错误