前面我们讲了java操作json的一些基本操作,现在就将他应用于实战中:

 

  1. package com.lyqf; 
  2.  
  3. import java.io.BufferedReader; 
  4. import java.io.FileNotFoundException; 
  5. import java.io.IOException; 
  6. import java.io.InputStreamReader; 
  7. import java.net.SocketTimeoutException; 
  8. import java.net.URL; 
  9. import java.net.URLConnection; 
  10.  
  11. import net.sf.json.JSONObject; 
  12.  
  13. public class Weather { 
  14.     String Ctiyid; 
  15.     URLConnection connectionData; 
  16.     StringBuilder sb; 
  17.     BufferedReader br;// 读取data数据流 
  18.     JSONObject jsonData; 
  19.     JSONObject info; 
  20.      
  21.     //从天气网解析的参数 
  22.     String city ;// 城市 
  23.     String date_y;//日期 
  24.     String week ;// 星期 
  25.     String fchh ;// 发布时间 
  26.      
  27.     String weather1 ;// 未来1到6天天气情况 
  28.     String weather2 ; 
  29.     String weather3 ; 
  30.     String weather4 ; 
  31.     String weather5 ; 
  32.     String weather6 ; 
  33.      
  34.     String wind1;//未来1到6天风况 
  35.     String wind2; 
  36.     String wind3; 
  37.     String wind4; 
  38.     String wind5; 
  39.     String wind6; 
  40.      
  41.     String fl1;//风的等级 
  42.     String fl2; 
  43.     String fl3; 
  44.     String fl4; 
  45.     String fl5; 
  46.     String fl6; 
  47.      
  48.      
  49.     String temp1 ;// 未来1到6天的气温 
  50.     String temp2 ; 
  51.     String temp3 ; 
  52.     String temp4 ; 
  53.     String temp5 ; 
  54.     String temp6 ; 
  55.      
  56.     String index;// 今天的穿衣指数 
  57.     String index_uv ;// 紫外指数 
  58.     String index_tr ;// 旅游指数 
  59.     String index_co ;// 舒适指数 
  60.     String index_cl ;// 晨练指数 
  61.     String index_xc;//洗车指数 
  62.     String index_d;//天气详细穿衣指数 
  63.      
  64.   
  65.      
  66.     public Weather(String Cityid) throws IOException ,NullPointerException{ 
  67.         // 解析本机ip地址 
  68.  
  69.         this.Ctiyid = Cityid; 
  70.         // 连接中央气象台的API 
  71.         URL url = new URL("http://m.weather.com.cn/data/" + Ctiyid + ".html"); 
  72.         connectionData = url.openConnection(); 
  73.         connectionData.setConnectTimeout(1000); 
  74.         try { 
  75.             br = new BufferedReader(new InputStreamReader( 
  76.                     connectionData.getInputStream(), "UTF-8")); 
  77.             sb = new StringBuilder(); 
  78.             String line = null
  79.             while ((line = br.readLine()) != null
  80.                 sb.append(line); 
  81.         } catch (SocketTimeoutException e) { 
  82.             System.out.println("连接超时"); 
  83.         } catch (FileNotFoundException e) { 
  84.             System.out.println("加载文件出错"); 
  85.         } 
  86.             String datas = sb.toString();        
  87.            jsonData = JSONObject.fromObject(datas); 
  88.            info = jsonData.getJSONObject("info"); 
  89.          
  90.         city = info.getString("city").toString(); 
  91.         week =  info.getString("week").toString(); 
  92.         date_y = info.getString("date_y").toString(); 
  93.         fchh = info.getString("fchh").toString(); 
  94.         //1到6天的天气 
  95.         weather1 =  info.getString("weather1").toString(); 
  96.         weather2 =  info.getString("weather2").toString(); 
  97.         weather3 =  info.getString("weather3").toString(); 
  98.         weather4 =  info.getString("weather4").toString(); 
  99.         weather5 =  info.getString("weather5").toString(); 
  100.         weather6 =  info.getString("weather6").toString(); 
  101.         //1到6天的气温 
  102.         temp1 = info.getString("temp1").toString(); 
  103.         temp2 = info.getString("temp2").toString(); 
  104.         temp3 = info.getString("temp3").toString(); 
  105.         temp4 = info.getString("temp4").toString(); 
  106.         temp5 = info.getString("temp5").toString(); 
  107.         temp6 = info.getString("temp6").toString(); 
  108.         //1到6天的风况 
  109.         wind1 = info.getString("wind1").toString(); 
  110.         wind2 = info.getString("wind2").toString(); 
  111.         wind3 = info.getString("wind3").toString(); 
  112.         wind4 = info.getString("wind4").toString(); 
  113.         wind5 = info.getString("wind5").toString(); 
  114.         wind6 = info.getString("wind6").toString(); 
  115.         //1到6天的风速 
  116.         fl1 = info.getString("fl1").toString(); 
  117.         fl2 = info.getString("fl2").toString(); 
  118.         fl3 = info.getString("fl3").toString(); 
  119.         fl4 = info.getString("fl4").toString(); 
  120.         fl5 = info.getString("fl5").toString(); 
  121.         fl6 = info.getString("fl6").toString(); 
  122.         //各种天气指数 
  123.         index = info.getString("index").toString(); 
  124.         index_uv = info.getString("index_uv").toString(); 
  125.         index_tr = info.getString("index_tr").toString(); 
  126.         index_co= info.getString("index_co").toString(); 
  127.         index_cl = info.getString("index_cl").toString(); 
  128.         index_xc = info.getString("index_xc").toString(); 
  129.         index_d =  info.getString("index_d").toString(); 
  130.  
  131.     } 
  132.     public static void main(String[] args) { 
  133.         try { 
  134.             new Weather("101270803"); // 101270803就是你的城市代码
  135.         } catch (NullPointerException e) { 
  136.             // TODO Auto-generated catch block 
  137.             e.printStackTrace(); 
  138.         } catch (IOException e) { 
  139.             // TODO Auto-generated catch block 
  140.             e.printStackTrace(); 
  141.         } 
  142.     } 

  URL url = new URL("http://m.weather.com.cn/data/" + Ctiyid + ".html"); 这里就是中国天气网数据页面,然后我们

URLConnection类的openConnection方法得到连接,再通过流的形行将其写到我们的StringBuilder之中。他是一串json的数据格式,

所以我们就须要将他转正json,

  1.  jsonData = JSONObject.fromObject(datas); 
  2.            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":"极不易发"}}