1. 程式人生 > >天氣預報的介面 josn 進行企業級的解析

天氣預報的介面 josn 進行企業級的解析

 在以前 有一個天氣預報的介面,是json型別的,資料比較大,不會進行解析,功能沒有做出來,今天偶然的學習中,可以拿出來了

   api返回的資料: 

     {     "date": "20180805",     "message": "Success !",     "status": 200,     "city": "北京",     "count": 1,     "data": {         "shidu": "66%",         "pm25": 60,         "pm10": 103,         "quality": "輕度汙染",         "wendu": "31",         "ganmao": "兒童、老年人及心臟、呼吸系統疾病患者人群應減少長時間或高強度戶外鍛鍊",         "yesterday": {             "date": "04日星期六",             "sunrise": "05:14",             "high": "高溫 36.0℃",             "low": "低溫 27.0℃",             "sunset": "19:26",             "aqi": 126,             "fx": "南風",             "fl": "<3級",             "type": "晴",             "notice": "願你擁有比陽光明媚的心情"         },         "forecast": [             {                 "date": "05日星期日",                 "sunrise": "05:15",                 "high": "高溫 35.0℃",                 "low": "低溫 25.0℃",                 "sunset": "19:25",                 "aqi": 91,                 "fx": "東風",                 "fl": "<3級",                 "type": "雷陣雨",                 "notice": "帶好雨具,別在樹下躲雨"             },             {                 "date": "06日星期一",                 "sunrise": "05:16",                 "high": "高溫 31.0℃",                 "low": "低溫 25.0℃",                 "sunset": "19:24",                 "aqi": 90,                 "fx": "東風",                 "fl": "<3級",                 "type": "雷陣雨",                 "notice": "帶好雨具,別在樹下躲雨"             },             {                 "date": "07日星期二",                 "sunrise": "05:17",                 "high": "高溫 30.0℃",                 "low": "低溫 24.0℃",                 "sunset": "19:22",                 "aqi": 98,                 "fx": "東風",                 "fl": "<3級",                 "type": "雷陣雨",                 "notice": "帶好雨具,別在樹下躲雨"             },             {                 "date": "08日星期三",                 "sunrise": "05:18",                 "high": "高溫 30.0℃",                 "low": "低溫 24.0℃",                 "sunset": "19:21",                 "aqi": 52,                 "fx": "東風",                 "fl": "<3級",                 "type": "雷陣雨",                 "notice": "帶好雨具,別在樹下躲雨"             },             {                 "date": "09日星期四",                 "sunrise": "05:19",                 "high": "高溫 31.0℃",                 "low": "低溫 24.0℃",                 "sunset": "19:20",                 "aqi": 64,                 "fx": "南風",                 "fl": "<3級",                 "type": "小雨",                 "notice": "雨雖小,注意保暖別感冒"             }         ]     } }

程式碼的實現weather

public class Weather {     private String date;     private String sunrise;     private String high;     private String low;     private String sunset;     private int aqi;     private String fx;     private String fl;     private String type;     private String notice;

    public Weather(String date, String sunrise, String high, String low, String sunset, int aqi, String fx, String fl, String type, String notice) {         this.date = date;         this.sunrise = sunrise;         this.high = high;         this.low = low;         this.sunset = sunset;         this.aqi = aqi;         this.fx = fx;         this.fl = fl;         this.type = type;         this.notice = notice;     }

    public String getDate() {         return date;     }

    public void setDate(String date) {         this.date = date;     }

    public String getSunrise() {         return sunrise;     }

    public void setSunrise(String sunrise) {         this.sunrise = sunrise;     }

    public String getHigh() {         return high;     }

    public void setHigh(String high) {         this.high = high;     }

    public String getLow() {         return low;     }

    public void setLow(String low) {         this.low = low;     }

    public String getSunset() {         return sunset;     }

    public void setSunset(String sunset) {         this.sunset = sunset;     }

    public int getAqi() {         return aqi;     }

    public void setAqi(int aqi) {         this.aqi = aqi;     }

    public String getFx() {         return fx;     }

    public void setFx(String fx) {         this.fx = fx;     }

    public String getFl() {         return fl;     }

    public void setFl(String fl) {         this.fl = fl;     }

    public String getType() {         return type;     }

    public void setType(String type) {         this.type = type;     }

    public String getNotice() {         return notice;     }

    public void setNotice(String notice) {         this.notice = notice;     } }

程式碼的實現;weathdata

public class WeatherData {     @JSONField(ordinal = 1)     private String shidu;     @JSONField(ordinal = 2)     private int pm25;     @JSONField(ordinal = 3)     private int pm10;     @JSONField(ordinal = 4)     private String quality;     @JSONField(ordinal = 5)     private String wendu;     @JSONField(ordinal = 6)     private String ganmao;     @JSONField(ordinal = 7)     private Weather yesterday;     @JSONField(ordinal = 8)     private List<Weather> forecast;

    public String getShidu() {         return shidu;     }

    public void setShidu(String shidu) {         this.shidu = shidu;     }

    public int getPm25() {         return pm25;     }

    public void setPm25(int pm25) {         this.pm25 = pm25;     }

    public int getPm10() {         return pm10;     }

    public void setPm10(int pm10) {         this.pm10 = pm10;     }

    public String getQuality() {         return quality;     }

    public void setQuality(String quality) {         this.quality = quality;     }

    public String getWendu() {         return wendu;     }

    public void setWendu(String wendu) {         this.wendu = wendu;     }

    public String getGanmao() {         return ganmao;     }

    public void setGanmao(String ganmao) {         this.ganmao = ganmao;     }

    public Weather getYesterday() {         return yesterday;     }

    public void setYesterday(Weather yesterday) {         this.yesterday = yesterday;     }

    public List<Weather> getForecast() {         return forecast;     }

    public void setForecast(List<Weather> forecast) {         this.forecast = forecast;     } }

  使用的是三層結構    ---->servlet

@WebServlet("/weather") public class WeatherServlet extends HttpServlet {     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {         doGet(request, response);     }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {         response.setContentType("text/html;charset=utf-8");         Weather weather = new Weather("04日星期六","05:14","高溫 36.0℃","低溫 27.0℃","19:26",126,"南風","<3級","晴","願你擁有比陽光明媚的心情");         Weather weather1 = new Weather("05日星期日","05:14","高溫 36.0℃","低溫 27.0℃","19:26",126,"南風","<3級","晴","願你擁有比陽光明媚的心情");         Weather weather2 = new Weather("06日星期一","05:14","高溫 36.0℃","低溫 27.0℃","19:26",126,"南風","<3級","晴","願你擁有比陽光明媚的心情");         Weather weather3 = new Weather("07日星期二","05:14","高溫 36.0℃","低溫 27.0℃","19:26",126,"南風","<3級","晴","願你擁有比陽光明媚的心情");         Weather weather4 = new Weather("08日星期三","05:14","高溫 36.0℃","低溫 27.0℃","19:26",126,"南風","<3級","晴","願你擁有比陽光明媚的心情");         Weather weather5 = new Weather("09日星期四","05:14","高溫 36.0℃","低溫 27.0℃","19:26",126,"南風","<3級","晴","願你擁有比陽光明媚的心情");         WeatherData weatherData = new WeatherData();         weatherData.setShidu("66%");         weatherData.setPm25(60);         weatherData.setPm10(103);         weatherData.setQuality("輕度汙染");         weatherData.setWendu("31");         weatherData.setGanmao("兒童、老年人及心臟、呼吸系統疾病患者人群應減少長時間或高強度戶外鍛鍊");         weatherData.setYesterday(weather);         List<Weather> list = new ArrayList<>();         list.add(weather1);         list.add(weather2);         list.add(weather3);         list.add(weather4);         list.add(weather5);         weatherData.setForecast(list);

        Map<String,Object> map = new HashMap<>();         map.put("data",weatherData);         String jsonString = JSON.toJSONString(map);         response.getWriter().print(jsonString);     } }

------------------------------------->