RestTemplate restTemplate = new RestTemplate();
Map<String,String> map = new HashMap<String,String>();
map.put("district_id","这里填入的是你想要查询的城市或地区的六位数字地址码(简单说就是这个地区的身份证开头六位)");
map.put("data_type","all");//这个是数据类型
map.put("ak","这个填入的是你百度地图的访问AK");
String res = restTemplate.getForObject(
"https://api.map.baidu.com/weather/v1/?district_id={district_id}&data_type={data_type}&ak={ak}",
String.class,
map);
JSONObject json = JSONObject.parseObject(res);//
下面是以南京栖霞区为例获得返回结果的数据格式如下:
{
"status": 0,
"result": {
"location": {
"country": "中国",
"province": "江苏省",
"city": "南京市",
"name": "栖霞区",
"id": "320113"
},
"now": {
"text": "晴",
"temp": 9,
"feels_like": 8,
"rh": 37,
"wind_class": "2级",
"wind_dir": "东南风",
"uptime": "20211213162000"
},
"forecasts": [{
"text_day": "晴",
"text_night": "晴",
"high": 10,
"low": 1,
"wc_day": "<3级",
"wd_day": "东风",
"wc_night": "<3级",
"wd_night": "东南风",
"date": "2021-12-13",
"week": "星期一"
}, {
"text_day": "多云",
"text_night": "多云",
"high": 14,
"low": 5,
"wc_day": "<3级",
"wd_day": "南风",
"wc_night": "<3级",
"wd_night": "东南风",
"date": "2021-12-14",
"week": "星期二"
}, {
"text_day": "多云",
"text_night": "小雨",
"high": 15,
"low": 9,
"wc_day": "<3级",
"wd_day": "东北风",
"wc_night": "3~4级",
"wd_night": "东北风",
"date": "2021-12-15",
"week": "星期三"
}, {
"text_day": "小雨",
"text_night": "小雨",
"high": 11,
"low": 4,
"wc_day": "3~4级",
"wd_day": "北风",
"wc_night": "3~4级",
"wd_night": "西北风",
"date": "2021-12-16",
"week": "星期四"
}, {
"text_day": "多云",
"text_night": "多云",
"high": 9,
"low": -2,
"wc_day": "4~5级",
"wd_day": "北风",
"wc_night": "<3级",
"wd_night": "东北风",
"date": "2021-12-17",
"week": "星期五"
}]
},
"message": "success"
}