json解析天气预报java_java接口获取天气预报json数据

https://blog.csdn.net/weixin_38959210/article/details/83515783

首先进上面这个链接的文档里面,获取到JAVA调用HTTP接口的方法。

我们用的这个http接口获取天气情况"http://t.weather.sojson.com/api/weather/city/101170301",后面的"101170301"为城市编号。

各城市编号连接下载地址"https://download.csdn.net/download/weixin_38959210/10793554";

@RequestMapping(value = "/tianqi.do")

public JSONObject tianqi(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {

request.setCharacterEncoding("utf-8");

response.setCharacterEncoding("UTF-8");

System.out.println("/tianqi.do打印出的东西:"+cn.temptation.web.tianqi.sendGet("http://t.weather.sojson.com/api/weather/city/101170301"));

return (JSONObject) JSON.parse(cn.temptation.web.tianqi.sendGet("http://t.weather.sojson.com/api/weather/city/101170301"));

}

以上为后台代码:

前台html代码为:

城市

更新时间

湿度

pm2.5

pm10

空气质量

温度

空气指数

js代码为:

$(function(){

function tianqi(){

$.ajax({

url: "tianqi.do",

type: "POST",

datatype: "json",

contentType: "application/json",

success: function(data){

$("#time").html(data.time);

$("#city").html(data.cityInfo.city);

$("#updateTime").html(data.cityInfo.updateTime);

$("#shidu").html(data.data.shidu);

$("#pm25").html(data.data.pm25);

$("#pm10").html(data.data.pm10);

$("#quality").html(data.data.quality);

$("#wendu").html(data.data.wendu);

$("#ganmao").html(data.data.ganmao);

},

error:function(){

alert("获取数据失败");

}

});

}

下面这个就是显示效果:

492be0851f3d9d6713ad7ab27793c782.png

看完文章了可以试试下面这个接口:

http://i.tianqi.com/index.php?c=code&id=12&icon=1&num=5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值