怎么獲取pm2.5數據----pm2.5 的獲取 java 通過url獲取后,得到json 格式,在解析json


//石家庄鐵道大學    tbh     基於雲計算的pm2.5監控系統,    通過  api 獲取 pm2.5的數據

//PM2.5數據以經由互聯網抓取各城市的PM 2.5等數據,或者直接調用部分網站開放的API接口獲取各城市的PM2.5等數據,如www.pm25.in  、等多個網站都開放了pm2.5的數據接口、天氣等的實時查詢API。

//解析json數據的時候會用到json 的jar包,可以網上下載

 

 

 //包結構

//pm25.java

 1 package com.tbh.entity;  2 //json 格式數據的對象的實體類
 3 public class pm25 {  4 //aqi指數
 5     private String aqi;  6     //地域名稱
 7     private String area;  8     //顆粒物(粒徑小於等於2.5μm)1小時平均
 9     private String pm2_5; 10     //顆粒物(粒徑小於等於2.5μm)24小時滑動平均
11     private String pm2_5_24h; 12     //監測點名稱
13     private String position_name; 14     //首要污染物
15     private String primary_pollutant; 16     //空氣質量指數類別,有“優、良、輕度污染、中度污染、重度污染、嚴重污染”6類
17     private String quality; 18     //監測點編碼
19     private String station_code; 20     //數據發布的時間
21     private String time_point; 22     public String getAqi() { 23         return aqi; 24  } 25     public void setAqi(String aqi) { 26         this.aqi = aqi; 27  } 28     public String getArea() { 29         return area; 30  } 31     public void setArea(String area) { 32         this.area = area; 33  } 34     public String getPm2_5() { 35         return pm2_5; 36  } 37     public void setPm2_5(String pm2_5) { 38         this.pm2_5 = pm2_5; 39  } 40     public String getPm2_5_24h() { 41         return pm2_5_24h; 42  } 43     public void setPm2_5_24h(String pm2_5_24h) { 44         this.pm2_5_24h = pm2_5_24h; 45  } 46     public String getPosition_name() { 47         return position_name; 48  } 49     public void setPosition_name(String position_name) { 50         this.position_name = position_name; 51  } 52     public String getPrimary_pollutant() { 53         return primary_pollutant; 54  } 55     public void setPrimary_pollutant(String primary_pollutant) { 56         this.primary_pollutant = primary_pollutant; 57  } 58     public String getQuality() { 59         return quality; 60  } 61     public void setQuality(String quality) { 62         this.quality = quality; 63  } 64     public String getStation_code() { 65         return station_code; 66  } 67     public void setStation_code(String station_code) { 68         this.station_code = station_code; 69  } 70     public String getTime_point() { 71         return time_point; 72  } 73     public void setTime_point(String time_point) { 74         this.time_point = time_point; 75  } 76  @Override 77     public String toString() { 78         System.out.println( "pm25 [aqi=" + aqi + ", area=" + area + ", pm2_5=" + pm2_5 79                 + ", pm2_5_24h=" + pm2_5_24h + ", position_name="
80                 + position_name + ", primary_pollutant=" + primary_pollutant 81                 + ", quality=" + quality + ", station_code=" + station_code 82                 + ", time_point=" + time_point + "]"); 83         return null; 84  } 85     
86     
87 }

 //Pm25getjson.java   //通過url 得到 json數據

 1 package com.tbh.services;  2 
 3 import java.io.BufferedReader;  4 import java.io.IOException;  5 import java.io.InputStreamReader;  6 import java.net.MalformedURLException;  7 import java.net.URL;  8 import java.net.URLConnection;  9 
10 public class Pm25getjson { 11 
12     private String json; 13         public Pm25getjson(String url) 14  { 15              StringBuilder jsonbuilder = new StringBuilder(); 16            try { 17                 URL urlObject = new URL(url); 18                 URLConnection uc = urlObject.openConnection(); 19                 BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "UTF-8")); 20                 String inputLine = null; 21                 while ( (inputLine = in.readLine()) != null) { 22  jsonbuilder.append(inputLine); 23  } 24  in.close(); 25             } catch (MalformedURLException e) { 26  e.printStackTrace(); 27             } catch (IOException e) { 28  e.printStackTrace(); 29  } 30          json=jsonbuilder.toString(); 31  } 32         public String getJson() { 33             return json; 34  } 35         public void setJson(String json) { 36             this.json = json; 37  } 38         
39 }

//pm25service.java// 通過 獲取的json格式的數據, 解析成 對象

 1 package com.tbh.services;  2 
 3 import org.json.JSONArray;  4 import org.json.JSONException;  5 import org.json.JSONObject;  6 
 7 import com.tbh.entity.pm25;  8 
 9 
10 public class pm25service { 11 
12     
13 
14 
15     public pm25[] pm25arr_return(String url) 16  { 17         
18          Pm25getjson a=new Pm25getjson(url); 19          String json=a.getJson(); 20         JSONArray jsonArray = null; 21                     try { 22                         jsonArray = new JSONArray(json); 23                     } catch (JSONException e) { 24                         // TODO Auto-generated catch block
25  e.printStackTrace(); 26  } 27                     int iSize = jsonArray.length(); 28                     pm25[] pm25arr=new pm25[iSize]; 29                     System.out.println("Size:" + iSize); 30                     for (int i = 0; i < iSize; i++) { 31                      pm25arr[i]=new pm25(); 32                     
33                     try { 34                         System.out.println(jsonArray.getJSONObject(i).get("aqi").toString()); 35                         pm25arr[i].setAqi(jsonArray.getJSONObject(i).get("aqi").toString()); 36                     } catch (JSONException e) { 37                         // TODO Auto-generated catch block
38  e.printStackTrace(); 39  } 40                     try { 41                         pm25arr[i].setArea(jsonArray.getJSONObject(i).get("area").toString()); 42                     } catch (JSONException e) { 43                         // TODO Auto-generated catch block
44  e.printStackTrace(); 45  } 46                     try { 47                         pm25arr[i].setPm2_5(jsonArray.getJSONObject(i).get("pm2_5").toString()); 48                     } catch (JSONException e) { 49                         // TODO Auto-generated catch block
50  e.printStackTrace(); 51  } 52                     try { 53                         pm25arr[i].setPm2_5_24h(jsonArray.getJSONObject(i).get("pm2_5_24h").toString()); 54                     } catch (JSONException e) { 55                         // TODO Auto-generated catch block
56  e.printStackTrace(); 57  } 58                     try { 59                         pm25arr[i].setPrimary_pollutant(jsonArray.getJSONObject(i).get("position_name").toString()); 60                     } catch (JSONException e) { 61                         // TODO Auto-generated catch block
62  e.printStackTrace(); 63  } 64                     try { 65                         pm25arr[i].setQuality(jsonArray.getJSONObject(i).get("quality").toString()); 66                     } catch (JSONException e) { 67                         // TODO Auto-generated catch block
68  e.printStackTrace(); 69  } 70                     try { 71                         pm25arr[i].setStation_code(jsonArray.getJSONObject(i).get("station_code").toString()); 72                     } catch (JSONException e) { 73                         // TODO Auto-generated catch block
74  e.printStackTrace(); 75  } 76                     try { 77                         pm25arr[i].setTime_point(jsonArray.getJSONObject(i).get("time_point").toString()); 78                     } catch (JSONException e) { 79                         // TODO Auto-generated catch block
80  e.printStackTrace(); 81  } 82                   
83                   
84  } 85                     return pm25arr; 86         
87         
88         
89         
90         
91         
92         
93         
94         
95         
96  } 97     
98 }

//pm25test.java //主函數類 ,

package com.tbh.test; import com.tbh.entity.pm25; import com.tbh.services.Pm25getjson; import com.tbh.services.pm25service; public class pm25test { /** * @param args */
    public static void main(String[] args) { // TODO Auto-generated method stub
     String url = "http://www.pm25.in/api/querys/pm2_5.json?city=weinan&token=5j1znBVAsnSf5xQyNQyq";

//http://www.pm25.in/api/querys.json 獲取實施了新《環境空氣質量標准》的城市列表,即有PM2.5數據的城市列表
//http://www.pm25.in/api/query。s/all_cities.json 獲取所有城市的空氣質量詳細數據
//http://www.pm25.in/api/querys/aqi_ranking.json 獲取全部城市的空氣質量指數(AQI)排行榜

 pm25service pm25services=new pm25service(); pm25[] pm25entity=pm25services.pm25arr_return(url); for(int i=0;i<pm25entity.length;i++) { System.out.println( pm25entity[i].getAqi()); System.out.println( pm25entity[i].getArea()); System.out.println( pm25entity[i].getPm2_5()); System.out.println( pm25entity[i].getPm2_5_24h()); System.out.println( pm25entity[i].getPosition_name()); System.out.println( pm25entity[i].getPrimary_pollutant()); System.out.println( pm25entity[i].getQuality()); System.out.println( pm25entity[i].getStation_code()); System.out.println( pm25entity[i].getTime_point()); System.out.println(); } /* Pm25getjson a=new Pm25getjson(url); System.out.println(a.getJson());*/ } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM