前面我们讲了java操作json的一些基本操作,现在就将他应用于实战中:
- package com.lyqf;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.SocketTimeoutException;
- import java.net.URL;
- import java.net.URLConnection;
- import net.sf.json.JSONObject;
- public class Weather {
- String Ctiyid;
- URLConnection connectionData;
- StringBuilder sb;
- BufferedReader br;// 读取data数据流
- JSONObject jsonData;
- JSONObject info;
- //从天气网解析的参数
- String city ;// 城市
- String date_y;//日期
- String week ;// 星期
- String fchh ;// 发布时间
- String weather1 ;// 未来1到6天天气情况
- String weather2 ;
- String weather3 ;
- String weather4 ;
- String weather5 ;
- String weather6 ;
- String wind1;//未来1到6天风况
- String wind2;
- String wind3;
- String wind4;
- String wind5;
- String wind6;
- String fl1;//风的等级
- String fl2;
- String fl3;
- String fl4;
- String fl5;
- String fl6;
- String temp1 ;// 未来1到6天的气温
- String temp2 ;
- String temp3 ;
- String temp4 ;
- String temp5 ;
- String temp6 ;
- String index;// 今天的穿衣指数
- String index_uv ;// 紫外指数
- String index_tr ;// 旅游指数
- String index_co ;// 舒适指数
- String index_cl ;// 晨练指数
- String index_xc;//洗车指数
- String index_d;//天气详细穿衣指数
- public Weather(String Cityid) throws IOException ,NullPointerException{
- // 解析本机ip地址
- this.Ctiyid = Cityid;
- // 连接中央气象台的API
- URL url = new URL("http://m.weather.com.cn/data/" + Ctiyid + ".html");
- connectionData = url.openConnection();
- connectionData.setConnectTimeout(1000);
- try {
- br = new BufferedReader(new InputStreamReader(
- connectionData.getInputStream(), "UTF-8"));
- sb = new StringBuilder();
- String line = null;
- while ((line = br.readLine()) != null)
- sb.append(line);
- } catch (SocketTimeoutException e) {
- System.out.println("连接超时");
- } catch (FileNotFoundException e) {
- System.out.println("加载文件出错");
- }
- String datas = sb.toString();
- jsonData = JSONObject.fromObject(datas);
- info = jsonData.getJSONObject("info");
- city = info.getString("city").toString();
- week = info.getString("week").toString();
- date_y = info.getString("date_y").toString();
- fchh = info.getString("fchh").toString();
- //1到6天的天气
- weather1 = info.getString("weather1").toString();
- weather2 = info.getString("weather2").toString();
- weather3 = info.getString("weather3").toString();
- weather4 = info.getString("weather4").toString();
- weather5 = info.getString("weather5").toString();
- weather6 = info.getString("weather6").toString();
- //1到6天的气温
- temp1 = info.getString("temp1").toString();
- temp2 = info.getString("temp2").toString();
- temp3 = info.getString("temp3").toString();
- temp4 = info.getString("temp4").toString();
- temp5 = info.getString("temp5").toString();
- temp6 = info.getString("temp6").toString();
- //1到6天的风况
- wind1 = info.getString("wind1").toString();
- wind2 = info.getString("wind2").toString();
- wind3 = info.getString("wind3").toString();
- wind4 = info.getString("wind4").toString();
- wind5 = info.getString("wind5").toString();
- wind6 = info.getString("wind6").toString();
- //1到6天的风速
- fl1 = info.getString("fl1").toString();
- fl2 = info.getString("fl2").toString();
- fl3 = info.getString("fl3").toString();
- fl4 = info.getString("fl4").toString();
- fl5 = info.getString("fl5").toString();
- fl6 = info.getString("fl6").toString();
- //各种天气指数
- index = info.getString("index").toString();
- index_uv = info.getString("index_uv").toString();
- index_tr = info.getString("index_tr").toString();
- index_co= info.getString("index_co").toString();
- index_cl = info.getString("index_cl").toString();
- index_xc = info.getString("index_xc").toString();
- index_d = info.getString("index_d").toString();
- }
- public static void main(String[] args) {
- try {
- new Weather("101270803"); // 101270803就是你的城市代码
- } catch (NullPointerException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
URL url = new URL("http://m.weather.com.cn/data/" + Ctiyid + ".html"); 这里就是中国天气网数据页面,然后我们
URLConnection类的openConnection方法得到连接,再通过流的形行将其写到我们的StringBuilder之中。他是一串json的数据格式,
所以我们就须要将他转正json,
- jsonData = JSONObject.fromObject(datas);
- info = jsonData.getJSONObject("info");
这里就是我们转化,然后info.getString("XX").toString(); 得到对应的数据了。
附:
你看我们直接输入:http://m.weather.com.cn/data/101270803.html就能看到里的数据是:{"weatherinfo":{"city":"武胜","city_en":"wusheng","date_y":"2012年12月25日","date":"","week":"星期二","fchh":"08","cityid":"101270803","temp1":"9℃~6℃","temp2":"8℃~5℃","temp3":"10℃~5℃","temp4":"8℃~4℃","temp5":"7℃~3℃","temp6":"7℃~3℃","tempF1":"48.2℉~42.8℉","tempF2":"46.4℉~41℉","tempF3":"50℉~41℉","tempF4":"46.4℉~39.2℉","tempF5":"44.6℉~37.4℉","tempF6":"44.6℉~37.4℉","weather1":"阴转小雨","weather2":"小雨转阴","weather3":"多云","weather4":"小雨","weather5":"多云","weather6":"多云","img1":"2","img2":"7","img3":"7","img4":"2","img5":"1","img6":"99","img7":"7","img8":"99","img9":"1","img10":"99","img11":"1","img12":"99","img_single":"2","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":"南风小于3级","wind2":"南风小于3级","wind3":"南风小于3级","wind4":"南风小于3级","wind5":"南风小于3级","wind6":"南风小于3级","fx1":"南风","fx2":"南风","fl1":"小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级","index":"凉","index_d":"天气凉,建议着厚外套加毛衣等春秋服装。年老体弱者宜着大衣、呢外套加羊毛衫。","index48":"凉","index48_d":"天气凉,建议着厚外套加毛衣等春秋服装。年老体弱者宜着大衣、呢外套加羊毛衫。","index_uv":"最弱","index48_uv":"最弱","index_xc":"不宜","index_tr":"适宜","index_co":"较舒适","st1":"10","st2":"6","st3":"8","st4":"3","st5":"10","st6":"5","index_cl":"不宜","index_ls":"不太适宜","index_ag":"极不易发"}}