JSON的定义:
一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性。业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据交换。JSON采用兼容性很高的文本格式,同时也具备类似于C语言体系的行为。 – Json.org
JSON Vs XML
1.JSON和XML的数据可读性基本相同
2.JSON和XML同样拥有丰富的解析手段
3.JSON相对于XML来讲,数据的体积小
4.JSON与JavaScript的交互更加方便
5.JSON对数据的描述性比XML较差
6.JSON的速度要远远快于XML.
运行效果图:
[img]
[img]http://dl.iteye.com/upload/attachment/0070/0412/ccec676a-d0ce-358b-b826-584e11de802b.jpg[/img]
[/img]
json数据结构图,数据有些多,截两张图:
[img]
[img]http://dl.iteye.com/upload/attachment/0070/0416/7ff40121-4f91-3f28-b49d-68473c42cdb6.jpg[/img]
[/img]
[img]
[img]http://dl.iteye.com/upload/attachment/0070/0418/89884cdf-a9a4-32d8-bce1-2155a725e1ab.jpg[/img]
[/img]
封装的一个JSONUtil工具类,代码如下:
编写主Activity代码MainActivity,代码如下:
main.xml
AndroidManifest.xml:
记得添加访问网络的权限!!!!!!!
一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性。业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据交换。JSON采用兼容性很高的文本格式,同时也具备类似于C语言体系的行为。 – Json.org
JSON Vs XML
1.JSON和XML的数据可读性基本相同
2.JSON和XML同样拥有丰富的解析手段
3.JSON相对于XML来讲,数据的体积小
4.JSON与JavaScript的交互更加方便
5.JSON对数据的描述性比XML较差
6.JSON的速度要远远快于XML.
运行效果图:
[img]
[img]http://dl.iteye.com/upload/attachment/0070/0412/ccec676a-d0ce-358b-b826-584e11de802b.jpg[/img]
[/img]
json数据结构图,数据有些多,截两张图:
[img]
[img]http://dl.iteye.com/upload/attachment/0070/0416/7ff40121-4f91-3f28-b49d-68473c42cdb6.jpg[/img]
[/img]
[img]
[img]http://dl.iteye.com/upload/attachment/0070/0418/89884cdf-a9a4-32d8-bce1-2155a725e1ab.jpg[/img]
[/img]
封装的一个JSONUtil工具类,代码如下:
package com.amaker.json;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
/**
* @author zzl
* Json封装的工具类.
*/
public class JSONUtil {
private static final String TAG = "JSONUtil";
/**
* 获取json内容
* @param url
* @return JSONArray
* @throws JSONException
* @throws ConnectionException
*/
public static JSONObject getJSON(String url) throws JSONException, Exception {
return new JSONObject(getRequest(url));
}
/**
* 向api发送get请求,返回从后台取得的信息。
*
* @param url
* @return String
*/
protected static String getRequest(String url) throws Exception {
return getRequest(url, new DefaultHttpClient(new BasicHttpParams()));
}
/**
* 向api发送get请求,返回从后台取得的信息。
*
* @param url
* @param client
* @return String
*/
protected static String getRequest(String url, DefaultHttpClient client) throws Exception {
String result = null;
int statusCode = 0;
HttpGet getMethod = new HttpGet(url);
Log.d(TAG, "do the getRequest,url="+url+"");
try {
//getMethod.setHeader("User-Agent", USER_AGENT);
HttpResponse httpResponse = client.execute(getMethod);
//statusCode == 200 正常
statusCode = httpResponse.getStatusLine().getStatusCode();
Log.d(TAG, "statuscode = "+statusCode);
//处理返回的httpResponse信息
result = retrieveInputStream(httpResponse.getEntity());
} catch (Exception e) {
Log.e(TAG, e.getMessage());
throw new Exception(e);
} finally {
getMethod.abort();
}
return result;
}
/**
* 处理httpResponse信息,返回String
*
* @param httpEntity
* @return String
*/
protected static String retrieveInputStream(HttpEntity httpEntity) {
int length = (int) httpEntity.getContentLength();
if (length < 0) length = 10000;
StringBuffer stringBuffer = new StringBuffer(length);
try {
InputStreamReader inputStreamReader = new InputStreamReader(httpEntity.getContent(), HTTP.UTF_8);
char buffer[] = new char[length];
int count;
while ((count = inputStreamReader.read(buffer, 0, length - 1)) > 0) {
stringBuffer.append(buffer, 0, count);
}
} catch (UnsupportedEncodingException e) {
Log.e(TAG, e.getMessage());
} catch (IllegalStateException e) {
Log.e(TAG, e.getMessage());
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
return stringBuffer.toString();
}
}
编写主Activity代码MainActivity,代码如下:
package com.amaker.json;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final String BASE_URL = "http://open.ifenghui.cn/mobilecms/interface/mobile.action?code=888032100100&m=get_coolimages&p=1&filter=all";
private TextView tv_n;
private TextView tv_error;
private TextView tv_p;
private TextView tv_pcount;
private TextView tv_author;
private ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
getJSON();
}
/**
* 实例化组件
*/
public void init(){
tv_n = (TextView) findViewById(R.id.textView0);
tv_error = (TextView) findViewById(R.id.textView1);
tv_p = (TextView) findViewById(R.id.textView2);
tv_pcount = (TextView) findViewById(R.id.textView3);
tv_author = (TextView) findViewById(R.id.textView4);
img = (ImageView) findViewById(R.id.imageView1);
}
/**
* 解析json数据
*/
public void getJSON(){
try {
//获取后台返回的Json对象
JSONObject mJsonObject = JSONUtil.getJSON(BASE_URL);
String n = mJsonObject.getString("n");
String error = mJsonObject.getString("error");
String p = mJsonObject.getString("p");
String pcount = mJsonObject.getString("pcount");
//获得图书数组
JSONArray mJsonArray = mJsonObject.getJSONArray("result");
//获取第一本图书
JSONObject firstBook = mJsonArray.getJSONObject(0);
String author = firstBook.getString("author");
tv_n.setText(n);
tv_error.setText(error);
tv_p.setText(p);
tv_pcount.setText(pcount);
tv_author.setText(author);
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView0"
android:text="@string/hello" />
<TextView
android:text="TextView"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="TextView"
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="TextView"
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="TextView"
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="wrap_content"
android:src="@drawable/icon"
android:layout_height="wrap_content"
android:id="@+id/imageView1" />
</LinearLayout>
AndroidManifest.xml:
记得添加访问网络的权限!!!!!!!
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amaker.json"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>