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;
}// 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 ++) {
// TODO:Traverse the jsonarray
}
// 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);
}// 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);
}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
Android json数据解析,布布扣,bubuko.com
原文:http://blog.csdn.net/wzy_1988/article/details/20496905