天气预报的接口 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);
    }
}


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

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值