从 URL 检索和显示 JSON 数据
Retrieving and displaying JSON data from URL
我正在尝试使用javascript和URL请求从JSON对象中检索和显示有关当前天气的信息:
http://free.worldweatheronline.com/feed/weather.ashx?q=de39ga&format=json&num_of_days=2&key=ec9c2dc5ba201904120805'
URL 中的数据如下所示:
{
"data": {
"current_condition": [
{
"cloudcover": "75",
"humidity": "88",
"observation_time": "03:30 PM",
"precipMM": "2.7",
"pressure": "1008",
"temp_C": "12",
"temp_F": "54",
"visibility": "8",
"weatherCode": "302",
"weatherDesc": [
{
"value": "Moderate rain"
}
],
"weatherIconUrl": [
{
"value": "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0018_cloudy_with_heavy_rain.png"
}
],
"winddir16Point": "SE",
"winddirDegree": "140",
"windspeedKmph": "17",
"windspeedMiles": "11"
}
],
"request": [
{
"query": "DE3",
"type": "Postcode"
}
],
"weather": [
{
"date": "2012-05-09",
"precipMM": "11.8",
"tempMaxC": "13",
"tempMaxF": "56",
"tempMinC": "12",
"tempMinF": "53",
"weatherCode": "266",
"weatherDesc": [
{
"value": "Light drizzle"
}
],
"weatherIconUrl": [
{
"value": "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0017_cloudy_with_light_rain.png"
}
],
"winddir16Point": "SE",
"winddirDegree": "141",
"winddirection": "SE",
"windspeedKmph": "12",
"windspeedMiles": "7"
},
{
"date": "2012-05-10",
"precipMM": "11.1",
"tempMaxC": "18",
"tempMaxF": "64",
"tempMinC": "6",
"tempMinF": "43",
"weatherCode": "353",
"weatherDesc": [
{
"value": "Light rain shower"
}
],
"weatherIconUrl": [
{
"value": "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0009_light_rain_showers.png"
}
],
"winddir16Point": "SSW",
"winddirDegree": "209",
"winddirection": "SSW",
"windspeedKmph": "30",
"windspeedMiles": "19"
}
]
}
}
我已经尝试了几个脚本来尝试获取数据并取出位以显示在div 中。第一个看起来像这样:
$.ajax({
url: "http://free.worldweatheronline.com/feed/weather.ashx?q=de39ga&format=json&num_of_days=2&key=ec9c2dc5ba201904120805"
dataType: 'json',
success: function(data) {
jQuery.each(data, function() {
alert("HELLO");
alert("Current Cloud Cover = " + this.data.current_condition.cloudcover);
alert("Current Humidity = " + this.data.current_condition.humidity);
});
}
});
第二个看起来像这样:
var postcode = document.getElementById("address").value;
function getWeather(userName, count) {
$.getJSON(
'http://free.worldweatheronline.com/feed/weather.ashx?q' + postcode + '&format=json&num_of_days=2&key=ec9c2dc5ba201904120805',
{},
showWeather,
//'jsonp'
);
}
function showWeather(day) {
var str = '<ul>';
str += '<h2>Tweets from ' + postcode + '</h2>';
var i = 0;
$.each(day, function(index, value) {
if (i == count) return;
var dt = new Date(value.date);
str += '<p>';
str += value.temp_C; //gets "text" from JSON
str += '</p>';
str += '';
str += '';
i++;
});
}
我想从 JSON URL 获取天气信息并在div 中显示一些信息,任何人都可以解释一下这两个脚本不起作用时如何做到这一点。
$.ajax({
url: "http://free.worldweatheronline.com/feed/weather.ashx?q=de39ga&format=json&num_of_days=2&key=ec9c2dc5ba201904120805",
dataType: 'jsonp', // You need to use 'jsonp' here because it is cross domain request
success: function(data) {
$.each(data, function(index, value) {
alert(value.current_condition[0].cloudcover);
alert(value.current_condition[0].humidity);
alert(value.current_condition[0].weatherDesc[0].value);
alert(value.request[0].query);
alert(value.request[0].query);
$.each(value.weather, function(i, val) {
alert(val.precipMM);
alert(val.weatherDesc[0].value);
})
});
}
});
演示
有几个问题...以下内容应该有效(修改第一个代码块)。
$.ajax({
url: "http://free.worldweatheronline.com/feed/weather.ashx?q=de39ga&format=json&num_of_days=2&key=ec9c2dc5ba201904120805&callback=?",
dataType: 'jsonp',
success: function(data){
jQuery.each(data, function(){
alert(JSON.stringify(this));
alert("Current Cloud Cover = " + this.current_condition[0].cloudcover);
alert("Current Humidity = " + this.current_condition[0].humidity);
});
}
});
回顾一下:
- 您需要使用 JsonP 来规避跨站点脚本限制(通过在 AJAX URL 中添加"&callback=?"来实现。
- JSON 响应的根是数据,因此您需要编写 data.data。
- current_condition属性是一个数组 - 必须添加一个索引器(即,[0])才能访问它。
相关文章:
- 如何将JSON数据导入我的ejs模板
- 如何将json数据显示为html
- Json数据包含日期和时间格式
- 用我的json数据填充JQuery DataTable
- AngularJS加载JSON数据,然后从中解析/加载HTML
- 使用jquery读取Json数据
- 如何将本地json数据加载到Extjs数据模型中
- 如何使用jquery ajax和jsonp在您自己的域上读取json数据
- React-将jSON数据传递给子组件的子组件
- 尝试从控制器加载带有json数据的Simile时间线
- Jquery:对返回JSON数据的php脚本的Ajax调用
- 如何通过数据分组将json数据转换为嵌套的json数据格式
- 如何在ReactJs中显示Json数据
- 使用javascript进行JSON数据检索
- tu如何将id放在填充了json数据的html表td上
- 将JSON数据从服务器加载到knockout.jsobservable中
- 使用JavaScript处理JSON数据
- 如何将json数据传递到.js文件
- 如何在带有nunjucks和gullow数据的JSON数据中使用带有HTML的内容
- 如何使用var名称调用JSON数据