日期:2014-05-16 浏览次数:20429 次
{"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":"基本适宜"}}
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