Json是一種與XML功能類似的作為資料轉送的工具,
解析區別:Json一行行讀取解析,而XML先整體後局部。
Json檔案一般格式如下:
{ "城市代碼": [ { "省": "北京", "市": [ { "市名": "北京", "編碼": "101010100" }, { "市名": "朝陽", "編碼": "101010300" }, { "市名": "順義", "編碼": "101010400" }, { "市名": "懷柔", "編碼": "101010500" }, { "市名": "通州", "編碼": "101010600" }, { "市名": "昌平", "編碼": "101010700" }, { "市名": "延慶", "編碼": "101010800" }, { "市名": "丰台", "編碼": "101010900" }, { "市名": "石景山", "編碼": "101011000" }, { "市名": "大興", "編碼": "101011100" }, { "市名": "房山", "編碼": "101011200" }, { "市名": "密雲", "編碼": "101011300" }, { "市名": "門頭溝", "編碼": "101011400" }, { "市名": "平穀", "編碼": "101011500" }, { "市名": "八達嶺", "編碼": "101011600" }, { "市名": "佛爺頂", "編碼": "101011700" }, { "市名": "湯河口", "編碼": "101011800" }, { "市名": "密雲上甸子", "編碼": "101011900" }, { "市名": "齋堂", "編碼": "101012000" }, { "市名": "霞雲嶺", "編碼": "101012100" }, { "市名": "北京城區", "編碼": "101012200" }, { "市名": "海澱", "編碼": "101010200" } ] }, { "省": "天津市", "市": [ { "市名": "天津", "編碼": "101030100" }, { "市名": "寶坻", "編碼": "101030300" }, { "市名": "東麗", "編碼": "101030400" }, { "市名": "西青", "編碼": "101030500" }, { "市名": "北辰", "編碼": "101030600" }, { "市名": "薊縣", "編碼": "101031400" }, { "市名": "漢沽", "編碼": "101030800" }, { "市名": "靜海", "編碼": "101030900" }, { "市名": "津南", "編碼": "101031000" }, { "市名": "塘沽", "編碼": "101031100" }, { "市名": "大港", "編碼": "101031200" }, { "市名": "武清", "編碼": "101030200" }, { "市名": "寧河", "編碼": "101030700" } ] }, { "省": "上海", "市": [ { "市名": "上海", "編碼": "101020100" }, { "市名": "寶山", "編碼": "101020300" }, { "市名": "嘉定", "編碼": "101020500" }, { "市名": "南匯", "編碼": "101020600" }, { "市名": "浦東", "編碼": "101021300" }, { "市名": "青浦", "編碼": "101020800" }, { "市名": "松江", "編碼": "101020900" }, { "市名": "奉賢", "編碼": "101021000" }, { "市名": "崇明", "編碼": "101021100" }, { "市名": "徐家匯", "編碼": "101021200" }, { "市名": "閔行", "編碼": "101020200" }, { "市名": "金山", "編碼": "101020700" } ] } ]}
var people = { "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" }, { "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" }, { "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" } ], "authors": [ { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" }, { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" }, { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" } ], "musicians": [ { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" }, { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" } ] }
android解析Json關鍵代碼舉例:
super.onCreate(savedInstanceState);super.setContentView(R.layout.main);mButton = (Button) findViewById(R.id.mybut) ;String str = "{\"data\":[{\"id\":1,\"name\":\"Anjoy\",\"age\":28},"+ "{\"id\":2,\"name\":\"鄭霞\",\"age\":28}],\"company\":\"騰銳資訊技術有限公司\"}";StringBuffer buf = new StringBuffer() ;try {Map<String, Object> result = this.parseJson(str) ;// 解析json資料文本buf.append("公司名稱:" + result.get("company") + "\n");List<Map<String,Object>> all = (List<Map<String,Object>>) result.get("data") ; Iterator<Map<String,Object>> iter = all.iterator() ;while(iter.hasNext()){Map<String,Object> map = iter.next() ;buf.append("ID編號:" + map.get("id") + ",姓名:" + map.get("name")+ ",年齡:" + map.get("age") + "\n");}} catch (Exception e) {e.printStackTrace();}mButton.setText(buf) ;}private Map<String, Object> parseJson(String data) throws Exception {Map<String, Object> allMap = new HashMap<String, Object>();JSONObject allData = new JSONObject(data) ;// 全部的內容變為一個項allMap.put("company", allData.getString("company")); // 取出項JSONArray jsonArr = allData.getJSONArray("data"); // 取出數組List<Map<String,Object>> all = new ArrayList<Map<String,Object>>() ;for (int i = 0; i < jsonArr.length(); i++) {Map<String, Object> map = new HashMap<String, Object>();JSONObject jsonObj = jsonArr.getJSONObject(i);map.put("id", jsonObj.getInt("id"));map.put("name", jsonObj.getString("name"));map.put("age", jsonObj.getInt("age"));all.add(map);}allMap.put("data", all) ;return allMap;}