和风天气API获取近6天天气并解析JSON

申请和风天气key以及获取所在地Location

和风天气接口API:https://dev.qweather.com/docs/api/

1、如何请求

public class WeatherApi2 {
    public static void main(String[] args) throws Exception {
        String url = "https://devapi.qweather.com/v7/weather/7d?location=城市代码&key=自己申请的key";
        RestTemplate restTemplate = new RestTemplate();
        byte[] oResult = restTemplate.exchange(url, HttpMethod.GET, null, byte[].class).getBody();
        String unGZipResult = unGZip(oResult);
        System.out.println(unGZipResult);
    }

    public static String unGZip(byte[] oResult) throws Exception {
        try (InputStream inputStream = new ByteArrayInputStream(oResult);
             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
             GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream)) {
            byte[] buf = new byte[4096];
            int len = -1;
            while ((len = gzipInputStream.read(buf, 0, buf.length)) != -1) {
                byteArrayOutputStream.write(buf, 0, len);
            }
            return new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            throw new Exception(e);
        }
    }

}

2、返回结果JSON类型

{
	"code": "200",
	"updateTime": "2023-07-05T21:35+08:00",
	"fxLink": "https://www.qweather.com/weather/kunshan-101190404.html",
	"daily": [{
		"fxDate": "2023-07-05",
		"sunrise": "04:57",
		"sunset": "19:06",
		"moonrise": "20:59",
		"moonset": "06:23",
		"moonPhase": "亏凸月",
		"moonPhaseIcon": "805",
		"tempMax": "33",
		"tempMin": "26",
		"iconDay": "101",
		"textDay": "多云",
		"iconNight": "305",
		"textNight": "小雨",
		"wind360Day": "234",
		"windDirDay": "西南风",
		"windScaleDay": "3-4",
		"windSpeedDay": "20",
		"wind360Night": "180",
		"windDirNight": "南风",
		"windScaleNight": "1-2",
		"windSpeedNight": "3",
		"humidity": "87",
		"precip": "0.0",
		"pressure": "1002",
		"vis": "25",
		"cloud": "25",
		"uvIndex": "12"
	}, {
		"fxDate": "2023-07-06",
		"sunrise": "04:57",
		"sunset": "19:06",
		"moonrise": "21:41",
		"moonset": "07:35",
		"moonPhase": "亏凸月",
		"moonPhaseIcon": "805",
		"tempMax": "34",
		"tempMin": "28",
		"iconDay": "302",
		"textDay": "雷阵雨",
		"iconNight": "104",
		"textNight": "阴",
		"wind360Day": "225",
		"windDirDay": "西南风",
		"windScaleDay": "1-2",
		"windSpeedDay": "3",
		"wind360Night": "225",
		"windDirNight": "西南风",
		"windScaleNight": "1-2",
		"windSpeedNight": "3",
		"humidity": "81",
		"precip": "10.9",
		"pressure": "999",
		"vis": "24",
		"cloud": "67",
		"uvIndex": "5"
	}],
	"refer": {
		"sources": ["QWeather", "NMC", "ECMWF"],
		"license": ["CC BY-SA 4.0"]
	}
}

3、解析JSON

创建JSON的返回的第一层对象

@Data
public class WeatherObject {
    private Object code;
    private Object updateTime;
    private Object fxLink;
    private Object daily;
    private Object refer;
}

创建daily集合的第二层对象(获取天气具体信息)

@Data
public class Weather {
   private String fxDate;
   private String textNight;
   private String tempMin;
   private String tempMax;
   private String windDirDay;
   private String windScaleDay;
}

a.利用FastJson,需要先导入FastJson的依赖包

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.76</version>
</dependency>

b.现在我们就可以解析啦(在1中的代码进行补充)~

    public static void main(String[] args) throws Exception {
        String url = "https://devapi.qweather.com/v7/weather/7d?location=所在地区&key=自己创建的key";
        RestTemplate restTemplate = new RestTemplate();
        byte[] oResult = restTemplate.exchange(url, HttpMethod.GET, null, byte[].class).getBody();
        String unGZipResult = unGZip(oResult);
        JSONObject jsonObject = new JSONObject(JSON.parseObject(unGZipResult));
        WeatherObject weatherObject = JSONObject.toJavaObject(jsonObject, WeatherObject.class);
        List<Weather> weatherList = JSONObject.parseArray(weatherObject.getDaily().toString(), Weather.class);
        for (int i = 0; i < weatherList.size()-1; i++) {
            System.out.println(weatherList.get(i).getFxDate()+"  "+weatherList.get(i).getTextNight()
                    +"  温度:"+weatherList.get(i).getTempMin()+"~"+weatherList.get(i).getTempMax()+"℃" +"  "+
                    weatherList.get(i).getWindDirDay()+"  "+weatherList.get(i).getWindScaleDay());
        }
    }

坑:url访问路径(免费订阅为:devapi.qweather.com)

输出结果:

2023-07-06  阴  温度:28~34℃  西南风  1-2
2023-07-07  大雨  温度:27~34℃  西南风  1-2
2023-07-08  小雨  温度:27~31℃  西南风  1-2
2023-07-09  雷阵雨  温度:26~31℃  南风  1-2
2023-07-10  多云  温度:27~36℃  西南风  1-2
2023-07-11  晴  温度:27~37℃  南风  1-2
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值