为自己的网站添加天气显示
360API接口模式:
其中的 地区编码 与中国天气网的地区编码是一样的。如果不设置这个参数,则默认显示本地的天气状况。如重庆地区编码为101040100,即url引用变成下面代码
https://cdn.weather.hao.360.cn/sed_api_weather_info.php?app=360chrome&code=101040100
以下代码是一段简单调用接口的示例,根据里面的参数调整成自己想要的数据格式即可。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>天气接口使用示例</title>
<style>
body {
font-family: microsoft yahei;
}
</style>
<script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<div id="output"></div>
<script type="text/javascript" charset="utf-8">
$.ajax({
type: "GET",
url: "http://cdn.weather.hao.360.cn/sed_api_weather_info.php?app=360chrome",
dataType : "jsonp",
jsonp: "_jsonp",//参数名
success: function(jsonData){
var html;
html = '数据更新时间:' + jsonData.pubdate + ' ' + jsonData.pubtime + '';
html += '地区:' + jsonData.area[0][0] + ' ' + jsonData.area[1][0] + ' ' + jsonData.area[2][0] + '';
html += '天气情况:';
for(var i =0; i<jsonData.weather.length; i++) {
html += jsonData.weather[i].date + '';
if(jsonData.weather[i].info.dawn !== undefined) {
html += '早晨天气:' + jsonData.weather[i].info.dawn[1] +
' 气温:' + jsonData.weather[i].info.dawn[0] + '~' + jsonData.weather[i].info.dawn[2] + '℃ ' +
jsonData.weather[i].info.dawn[3] +' '+ jsonData.weather[i].info.dawn[4] + '';
}
if(jsonData.weather[i].info.day !== undefined) {
html += '白天天气:' + jsonData.weather[i].info.day[1] +
' 气温:' + jsonData.weather[i].info.day[0] + '~' + jsonData.weather[i].info.day[2] + '℃ ' +
jsonData.weather[i].info.day[3] +' '+ jsonData.weather[i].info.day[4] + '';
}
if(jsonData.weather[i].info.night !== undefined) {
html += '夜间天气:' + jsonData.weather[i].info.night[1] +
' 气温:' + jsonData.weather[i].info.night[0] + '~' + jsonData.weather[i].info.night[2] + '℃ ' +
jsonData.weather[i].info.night[3] +' '+ jsonData.weather[i].info.night[4] + '';
}
html += '';
}
html += '穿衣:';
html += '【' + jsonData.life.info.chuanyi[0] + '】 ' + jsonData.life.info.chuanyi[1] + '';
html += '感冒:';
html += '【' + jsonData.life.info.ganmao[0] + '】 ' + jsonData.life.info.ganmao[1] + '';
html += '空调:';
html += '【' + jsonData.life.info.kongtiao[0] + '】 ' + jsonData.life.info.kongtiao[1] + '';
html += '污染:';
html += '【' + jsonData.life.info.wuran[0] + '】 ' + jsonData.life.info.wuran[1] + '';
html += '洗车:';
html += '【' + jsonData.life.info.xiche[0] + '】 ' + jsonData.life.info.xiche[1] + '';
html += '运动:';
html += '【' + jsonData.life.info.yundong[0] + '】 ' + jsonData.life.info.yundong[1] + '';
html += '紫外线:';
html += '【' + jsonData.life.info.ziwaixian[0] + '】 ' + jsonData.life.info.ziwaixian[1] + '';
html += 'PM2.5: ' + jsonData.pm25.pm25[0];
$("#output").html(html);
}
});
</script>
</body>
</html>
原作者: