从天气预报网页API获取天气预报数据

用Java代码获取中央气象局的天气预报数据

版权声明:数据版权归中央气象局所有,仅供学习交流之用。本文内容若有侵权,请联系我删除~

1 获取相关接口

通过解析html页面后,发现有如下3个接口:

  • 省份获取:http://www.nmc.cn/rest/province
  • 城市获取:http://www.nmc.cn/rest/province/{省份代码}
  • 城市天气预报获取:http://www.nmc.cn/rest/weather?stationid={城市代码}

2 省份获取接口数据

接口:http://www.nmc.cn/rest/province测试一下

返回数据:

[
    {"code":"ABJ","name":"北京市","url":"/publish/forecast/ABJ.html"},
    {"code":"ATJ","name":"天津市","url":"/publish/forecast/ATJ.html"},
    {"code":"AHE","name":"河北省","url":"/publish/forecast/AHE.html"},
    {"code":"ASC","name":"四川省","url":"/publish/forecast/ASC.html"},
    ...
    ...
    {"code":"ATW","name":"台湾省","url":"/publish/forecast/ATW.html"}
]

注意:这里的code键的值为城市获取的参数,如四川的参数为:ASC

3 城市获取接口数据

接口:http://www.nmc.cn/rest/province/ASC测试一下

[
    {"code":"56294","province":"四川省","city":"成都","url":"/publish/forecast/ASC/chengdu.html"},
    {"code":"56171","province":"四川省","city":"阿坝","url":"/publish/forecast/ASC/aba.html"},
    ...
    ...
    {"code":"56498","province":"四川省","city":"筠连","url":"/publish/forecast/ASC/yunlian.html"}
]

注意:这里的城市包括各市的区县,比如上面展示的筠连就是四川省宜宾市的辖县,这里code键的值就是访问城市天气数据接口的参数。

4 城市天气预报获取接口数据

接口:http://www.nmc.cn/rest/weather?stationid=56294测试一下

{
"msg":"success",
"code":0,
"data":
    {
        "real":
            {
                "station":{"code":"56294","province":"四川省","city":"成都","url":"/publish/forecast/ASC/chengdu.html"},
                "publish_time":"2020-07-26 22:25",
                "weather":{"temperature":24.9,"temperatureDiff":2.2,"airpressure":948.0,"humidity":81.0,"rain":0.0,"rcomfort":74,"icomfort":1,"info":"-","img":"9999","feelst":24.9},"wind":{"direct":"无持续风向","power":"微风","speed":""},"warn":{"alert":"9999","pic":"9999","province":"9999","city":"9999","url":"9999","issuecontent":"9999","fmeans":"9999","signaltype":"9999","signallevel":"9999","pic2":"9999"}
            },

        "predict":
            {
                "station":{"code":"56294","province":"四川省","city":"成都","url":"/publish/forecast/ASC/chengdu.html"},
                "publish_time":"2020-07-26 20:00",
                "detail":
                    [
                    {"date":"2020-07-26","pt":"2020-07-26 20:00", "day":{"weather":{"info":"9999","img":"9999","temperature":"9999"},"wind":{"direct":"9999","power":"9999"}},"night":{"weather":{"info":"晴","img":"0","temperature":"22"},"wind":{"direct":"无持续风向","power":"微风"}}},
                    {"date":"2020-07-27","pt":"2020-07-26 20:00","day":{"weather":{"info":"晴","img":"0","temperature":"34"},"wind":{"direct":"无持续风向","power":"微风"}},"night":{"weather":{"info":"晴","img":"0","temperature":"23"},"wind":{"direct":"无持续风向","power":"微风"}}},
                    ...
                    ...
                    {"date":"2020-08-01","pt":"2020-07-26 20:00","day":{"weather":{"info":"阵雨","img":"3","temperature":"30"},"wind":{"direct":"无持续风向","power":"微风"}},"night":{"weather":{"info":"阴","img":"2","temperature":"23"},"wind":{"direct":"无持续风向","power":"微风"}}}
                    ]
            },

        "air":{"forecasttime":"2020-07-26 21:00","aqi":65,"aq":2,"text":"良","aqiCode":"99052;99054;99055"},

        "tempchart":
            [
                {"time":"2020/07/19","max_temp":34.8,"min_temp":20.0,"day_img":"9999","day_text":"9999","night_img":"9999","night_text":"9999"},
                {"time":"2020/07/20","max_temp":29.4,"min_temp":23.8,"day_img":"9999","day_text":"9999","night_img":"9999","night_text":"9999"},
                {"time":"2020/07/21","max_temp":25.8,"min_temp":21.3,"day_img":"9999","day_text":"9999","night_img":"9999","night_text":"9999"},
                ...
                ...
                {"time":"2020/07/30","max_temp":26.7,"min_temp":22.5,"day_img":"10","day_text":"暴雨","night_img":"2","night_text":"阴"},
                {"time":"2020/07/31","max_temp":29.0,"min_temp":22.8,"day_img":"7","day_text":"小雨","night_img":"2","night_text":"阴"},
                {"time":"2020/08/01","max_temp":30.7,"min_temp":23.2,"day_img":"3","day_text":"阵雨","night_img":"2","night_text":"阴"}
            ],
        "passedchart":
            [
                {"rain1h":0.0,"rain24h":9999.0,"rain12h":9999.0,"rain6h":9999.0,"temperature":25.4,"tempDiff":"","humidity":76.0,"pressure":948.0,"windDirection":0.0,"windSpeed":0.0,"time":"2020-07-26 22:00"},
                {"rain1h":0.0,"rain24h":9999.0,"rain12h":9999.0,"rain6h":9999.0,"temperature":27.1,"tempDiff":"","humidity":70.0,"pressure":947.0,"windDirection":0.0,"windSpeed":0.0,"time":"2020-07-26 21:00"},
                {"rain1h":0.0,"rain24h":9999.0,"rain12h":9999.0,"rain6h":9999.0,"temperature":30.5,"tempDiff":"","humidity":55.0,"pressure":947.0,"windDirection":0.0,"windSpeed":0.0,"time":"2020-07-26 20:00"},
                ...
            ],

        "climate":
            {
                "time":"1981年-2010年",
                "month":
                    [
                        {"month":1,"maxTemp":9.1,"minTemp":2.6,"precipitation":8.7},
                        {"month":2,"maxTemp":11.4,"minTemp":4.8,"precipitation":12.4},
                        ...
                        {"month":12,"maxTemp":10.3,"minTemp":4.1,"precipitation":6.0}
                ]
            },

        "radar":{"title":"成都","image":"/product/2020/07/26/RDCP/SEVP_AOC_RDCP_SLDAS_EBREF_AZ9280_L88_PI_20200726144200000.PNG?v=1595774905591","url":"/publish/radar/si-chuan/cheng-du.htm"}
    }
}

注意:过期的数据值都为9999。

5 使用Java代码提取数据

这里用四川省成都市预报数据的tempchart键为例。获取步骤如下:

  1. 使用URL类打开链接
  2. 使用字节流读取数据
  3. 使用字符流将读取字节流中的数据
  4. 将字符流数据以字符的形式保存到StringBuilder字符构造类中
  5. 使用fastjson将StringBuilder保存的字符数据转换成json对象
  6. 使用fastjson中相关的方法提取需要的数据

Java代码:

public void getValue() {

    String url = "http://www.nmc.cn/rest/weather?stationid=56294";

    StringBuilder sb = new StringBuilder();//获取拼接读取到的json字符
    
    try {

        URL u = new URL(url);

        URLConnection con = u.openConnection();
        //字节输入流读取json数据,并将编码格式设置为utf8
        InputStreamReader in = new InputStreamReader(con.getInputStream(),
                StandardCharsets.UTF_8);
        //字符流读取输入流的数据
        BufferedReader br = new BufferedReader(in);

        String inputLine;//循环中间量

        while ((inputLine = br.readLine()) != null) {
            sb.append(inputLine);
        }
        br.close();
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    //将字符串转换为json对象
    JSONObject json = JSON.parseObject(sb.toString());
    // System.out.println(json);
    //获取data键的值,值为json格式,所以可以转换为json对象
    JSONObject data = json.getJSONObject("data");
    // System.out.println(data);
    //data键中的tempchar键的值是一个数组,所以使用JSONArray对象接收值
    JSONArray tempchart = data.getJSONArray("tempchart");
    // System.out.println(tempchart);
    //一共14条数据,过去7天和未来7天,这里只取未来7天数据。
    for (int i = 0; i < tempchart.size() - 7; i++) {
        JSONObject temp = tempchart.getJSONObject(i + 7);
        System.out.println("日期:" + temp.getString("time"));
        System.out.println("最高气温:" + temp.getString("max_temp"));
        System.out.println("最低气温:" + temp.getString("min_temp"));
        System.out.println("------------华丽的分割线-------------");
    }
}

运行结果:

日期:2020/07/27
最高气温:33.6
最低气温:23.0
------------华丽的分割线-------------
日期:2020/07/28
最高气温:34.7
最低气温:24.2
------------华丽的分割线-------------
日期:2020/07/29
最高气温:31.0
最低气温:22.1
------------华丽的分割线-------------
日期:2020/07/30
最高气温:26.7
最低气温:22.5
------------华丽的分割线-------------
日期:2020/07/31
最高气温:29.0
最低气温:22.8
------------华丽的分割线-------------
日期:2020/08/01
最高气温:30.7
最低气温:23.2
------------华丽的分割线-------------
日期:2020/08/02
最高气温:34.7
最低气温:24.0
------------华丽的分割线-------------

注意:

  • 根据返回值的格式一步一步获取想要的数据
  • 通过每次获取的结果,可以清楚的看出返回值的格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值