Android json数据解析

前言

喜欢在前言里讲一下自己的现状,或许能有共鸣的同学,更多的是留给自己一个纪念,几个月或者几年再回来看的时候还是会很有感慨。今天说说语言,json这种数据格式之前我做服务器端的时候天天接触,天真的以为json的世界里只有php的json_encode和json_decode,今天当我做客户端的时候,竟然将近一个多小时才搞定json的解析。这里我不是抨击php好坏,只是想说多学点东西,看看你不熟悉的领域,方能开阔自己的视野,方能知道天外有天,方能知道当初你看不上的工作其实你也不一定能很好的完成,扯多了,回来讲解json 

什么是json

json(Javascript Object Natation)是一种轻量级的数据交换格式,相比xml这种数据交换格式来说,json相对解析更加简单一些,因此客户端和服务器的数据交换格式往往通过json进行交换 
json一共有两种数据结构,一种是以(key/value)对形式存在的无序的jsonObject对象,一个对象以“{”(左花括号)开始,“}”(右花括号)结束。每个“名称”后跟一个“:”(冒号); “名称/值”对之间使用“,”(逗号) 
\

例如:{”name“:"zhengyi.wzy"},这就是一个最简单的json对象,对于这种数据格式,key值必须是String,而value则可以是String,Number,Object,Array等数据类型 
\

另一种数据格式就是有序的value的集合,这种形式被称为是jsonArray,数组是值(value)的有序集合。一个数组以”[“(左中括号)开始,”]“(右中括号)结束。值之间使用”,“(逗号)分隔 
vc/B6bvuo6y1sc7et6i78cihy/nWuLaoyv0mIzIwNTQwO8qxo6y9q7vht7W72NK7uPbErMjPJiMyMDU0MDujrLKisru74cXXs/bS7LOjo6i49sjLzca89sq508PV4rj2t723qKOpCjxicj4KCgo8aDM+SlNPTkFycmF5PC9oMz4Ky/y0+rHt0rvX6dPQ0PK1xMr9JiMyMDU0MDuho72rxuTXqru7zqpTdHJpbmfK5LP2KHRvU3RyaW5nKcv5se3P1rXE0M7KvcrH08O3vcCousWw/Ln8o6zK/SYjMjA1NDA70tS2urrFobEsobG31rj0o6jA/cjno7pbobFBbGliYWJhobAsobFCYWlkdaGwLKGwVGVjZW50obFdo6kKPGJyPgoK1eK49sDgtcTE2rK/zazR+b7f09Cy6dGv0NDOqqOsZ2V0KCm6zW9wdCgpwb3W1re9t6i2vL/J0tTNqLn9aW5kZXjL99L9t7W72Na4tqi1xMr9JiMyMDU0MDujrHB1dCgpt723qNPDwLTM7bzTu/LV38zmu7vK/SYjMjA1NDA7Cjxicj4KzazR+UpTT05BcnJhebXEdmFsdWXA4NDNv8nS1LD8wKijukpTT05PYmplY3RzLCBKU09OQXJyYXlzLCBTdHJpbmdzLCBCb29sZWFucywgSW50ZWdlcnMsIExvbmdzLCBEb3VibGVzIG9yIE5VTEw8YnI+Cgo8YnI+Cgo8YnI+Cgo8aDI+QW5kcm9pZL3izvbKtcD9PC9oMj4Kv827p7bL0ruw47TTt/7O8cb3u/G1w7a8ysfX1rf7tK6jrNaxvdNuZXfSu7j2SlNPTk9iamVjdLy0v8mjrLLOv7yzzNDyyOfPwgo8YnI+Cgo8aDM+u/HIoWpzb27X1rf7tK48L2gzPgrV4rj2zfjWt8zhuanBy2pzb27X1rf7tK6jumh0dHA6Ly9hcGkuYW5kcm9pZGhpdmUuaW5mby9jb250YWN0cy+jrCDO0sPHv8nS1L+qxvTSu7j2z9+zzLeixvBodHRwx+vH86Osu/HIoWpzb27X1rf7tK6jrLLOv7y0+sLryOfPwqO6Cjxicj4KCjxwcmUgY2xhc3M9"brush:java;">private String getJsonByNetwork() { // You can get json by this url final String url = "http://api.androidhive.info/contacts/"; DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); InputStream inputStream = null; String result = null; try { HttpResponse response = httpClient.execute(httpGet); inputStream = response.getEntity().getContent(); // Json is UTF-8 by default BufferedReader reader = new BufferedReader(new InputStreamReader( inputStream, "UTF-8")); StringBuilder sb = new StringBuilder(); String tmp = null; while ((tmp = reader.readLine()) != null) { sb.append(tmp); } result = sb.toString(); } catch (Exception e) { try { if (inputStream != null) { inputStream.close(); } } catch (IOException se) { } } return result; }

解析json字符串

解析json字符串的步骤一般如下所示: 
(1) 创建JSONObject: 

1
2
// Create a JSONObject
JSONObject jsonObject = new JSONObject(result);


(2) 获取JSONArray,遍历JSONArray数组 

1
2
3
4
5
6
7
// To get a specific JSONArray
JSONArray jsonArray = jsonObject.getJSONArray("contacts");
 
// To get items from the array
for (int i = 0; i < jsonArray.length(); i ++) {
    // TODO:Traverse the jsonarray
}


(3) 获取JSONObject 

1
2
3
4
5
6
7
8
9
10
11
// Create a JSONObject
JSONObject jsonObject = new JSONObject(result);
 
// To get a specific JSONArray
JSONArray jsonArray = jsonObject.getJSONArray("contacts");
 
// To get items from the array
for (int i = 0; i < jsonArray.length(); i++) {
    // To get a specific JSONObject
    JSONObject oneObject = jsonArray.getJSONObject(i);
}


(4)获取特定字符串 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Create a JSONObject
JSONObject jsonObject = new JSONObject(result);
 
// To get a specific JSONArray
JSONArray jsonArray = jsonObject.getJSONArray("contacts");
 
// To get items from the array
for (int i = 0; i < jsonArray.length(); i++) {
    // To get a specific JSONObject
    JSONObject oneObject = jsonArray.getJSONObject(i);
 
    // To get a specific string
    String id = oneObject.getString("id");
    String name = oneObject.getString("name");
 
    Log.e("wzy", "id is:" + id + ", name is " + name);
}


解析结果: 

1
2
3
4
5
6
7
8
9
10
11
12
13
03-05 10:26:08.690: E/wzy(26401): id is:c200, name is Ravi Tamada
03-05 10:26:08.690: E/wzy(26401): id is:c201, name is Johnny Depp
03-05 10:26:08.690: E/wzy(26401): id is:c202, name is Leonardo Dicaprio
03-05 10:26:08.690: E/wzy(26401): id is:c203, name is John Wayne
03-05 10:26:08.691: E/wzy(26401): id is:c204, name is Angelina Jolie
03-05 10:26:08.691: E/wzy(26401): id is:c205, name is Dido
03-05 10:26:08.691: E/wzy(26401): id is:c206, name is Adele
03-05 10:26:08.692: E/wzy(26401): id is:c207, name is Hugh Jackman
03-05 10:26:08.693: E/wzy(26401): id is:c208, name is Will Smith
03-05 10:26:08.693: E/wzy(26401): id is:c209, name is Clint Eastwood
03-05 10:26:08.694: E/wzy(26401): id is:c2010, name is Barack Obama
03-05 10:26:08.694: E/wzy(26401): id is:c2011, name is Kate Winslet
03-05 10:26:08.695: E/wzy(26401): id is:c2012, name is Eminem







posted @ 2014-03-10 10:36  huidaoli  阅读(457)  评论(0编辑  收藏  举报