日期:2014-05-16  浏览次数:20429 次

使用JACKSON解析JSON(HttpClient 3处理请求)
    在上一篇中,我们使用了XStream来解析XML(HttpClient 4发送请求)制作了一个电子商务小应用,本篇我们来解析另外一种使用广泛的数据交换技术JSON。
    本文意在说明JACKSON组件解析JSON格式文本,并结合中央气象台的天气预报API来展示。天气预报的请求地址是:http://m.weather.com.cn/data/101070201.html,这个文本就是城市代码,101070201代表大连市,执行URL,得到一个返回文本,是JSON格式的,如下:
{"weatherinfo":{"city":"大连","city_en":"dalian","date_y":"2010年11月21日","date":"庚寅年十月十六","week":"星期日","fchh":"18","cityid":"101070201","temp1":"0℃~6℃","temp2":"1℃~9℃","temp3":"4℃~7℃","temp4":"-1℃~6℃","temp5":"-1℃~7℃","temp6":"-2℃~3℃","tempF1":"32℉~42.8℉","tempF2":"33.8℉~48.2℉","tempF3":"39.2℉~44.6℉","tempF4":"30.2℉~42.8℉","tempF5":"30.2℉~44.6℉","tempF6":"28.4℉~37.4℉","weather1":"多云转晴","weather2":"晴","weather3":"多云","weather4":"晴","weather5":"晴转多云","weather6":"多云转晴","img1":"1","img2":"0","img3":"0","img4":"99","img5":"1","img6":"99","img7":"0","img8":"99","img9":"0","img10":"1","img11":"1","img12":"0","img_single":"0","img_title1":"多云","img_title2":"晴","img_title3":"晴","img_title4":"晴","img_title5":"多云","img_title6":"多云","img_title7":"晴","img_title8":"晴","img_title9":"晴","img_title10":"多云","img_title11":"多云","img_title12":"晴","img_title_single":"晴","wind1":"西北风5-6级转北风4-5级","wind2":"北风转南风4-5级","wind3":"南风4-5级转北风5-6级","wind4":"北风5-6级转4-5级","wind5":"北风转南风4-5级","wind6":"南风转北风4-5级","fx1":"西北风","fx2":"北风","fl1":"5-6级转4-5级","fl2":"4-5级","fl3":"4-5级转5-6级","fl4":"5-6级转4-5级","fl5":"4-5级","fl6":"4-5级","index":"凉","index_d":"天气凉,建议着厚外套加毛衣等春秋服装。年老体弱者宜着大衣、呢外套加羊毛衫。","index48":"凉","index48_d":"天气凉,建议着厚外套加毛衣等春秋服装。年老体弱者宜着大衣、呢外套加羊毛衫。","index_uv":"中等","index48_uv":"弱","index_xc":"较适宜","index_tr":"一般","index_co":"较不舒适","st1":"2","st2":"-4","st3":"7","st4":"1","st5":"5","st6":"1","index_cl":"较不宜","index_ls":"基本适宜"}}

    字段言简意赅,城市名,英文名,日期,农历日期,星期,预报时间,城市代码,6个预报温度(华氏温度,摄氏温度),6个预报天气和风力,剩下的就是一些气象指数什么的了,本例我们仅仅拿当日的信息来说明。
    这段文本是我们直接通过浏览器请求来的,但是在程序中如何进行?需要借助网络api的帮助,可以直接使用java网络api,也可以使用第三方类库,这里我们使用Apache的Commons组件中的HttpClient3来说明。首先建立项目工程,使用Maven管理:

    引入的必要的依赖,如下:

    Weather类是封装必要数据的Bean,很简单,如下:
package weather;
public class Weather {
	private String city;// 城市名
	private String date;// 日期:yyyy年MM月d日
	private String lunarDate;// 农历日期/当日有
	private String week;// 星期
	private String fcTime;// 预报时间:24制小时数/当日有
	private String temperature;// 当日气温
	private String weather;// 天气
	private String wind;// 风力
	// 省略了getter和setter方法
	@Override
	public String toString() {
		return "Weather [city=" + city + ", date=" + date + ", fcTime="
				+ fcTime + ", lunarDate=" + lunarDate + ", temperature="
				+ temperature + ", weather=" + weather + ", week=" + week
				+ ", wind=" + wind + "]";
	}
}

    下面就是示例代码了,很简单:
package weather;
import java.util.HashMap;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.MappingJsonFactory;
/**
 * 天气预报服务,解析JSON
 * 
 * @author Nanlei
 * 
 */
public class Demo {
	private static String URL = "http://m.weather.com.cn/data/101070201.html";// 请求的地址
	private static HttpClient client;
	private static GetMethod getMethod;
	/**
	 * 静态块初始化所需对象
	 */
	static {
		client = new HttpClient();
		getMethod = new GetMethod(URL);
	}
	/**
	 * 获取获得的Json结果
	 * 
	 * @return
	 */
	public static String getJsonText() {
		String jsonText = null;
		try {
			int status = client.executeMethod(getMethod);
			if (status == HttpStatus.SC_OK) {// HTTP 200 OK
				jsonText = getMethod.getResponseBodyAsString();// 获取字符串形式的结果,建议使用流式结果
			}
		} catch (Except