Angularjs 无法使用Angular从Json数据获取天气数据
解析方法错了吗?我正试图获取天气数据Angularjs 无法使用Angular从Json数据获取天气数据,angularjs,Angularjs,解析方法错了吗?我正试图获取天气数据 function Hello($scope, $http) { $http .jsonp('http://api.worldweatheronline.com/free/v1/weather.ashx?q=London&format=json&num_of_days=5&key=MYKEY&callback=JSON_CALLBACK') .success(function(data) { va
function Hello($scope, $http) {
$http
.jsonp('http://api.worldweatheronline.com/free/v1/weather.ashx?q=London&format=json&num_of_days=5&key=MYKEY&callback=JSON_CALLBACK')
.success(function(data) {
var datais = JSON.stringify(data);
console.log("Datais::"+datais);
console.log("Weather::"+datais["weather"]);
})
.error(function(data){
alert("Error");
});
}
输出:
我认为您需要通过以下方式了解天气:
console.log("Weather::"+datais.data.weather);
您可以使用Online json查看器查看数据结构:例如:或查看浏览器控制台。例如,firebug可以将json数据显示为树
您不需要JSON.stringify。Angular为您做了以下工作:
.success(function(data) {
console.log(data.data.weather);
})
在这里,您的datais变量被分配了数据。 如果您正确地检查了返回的json数据,正如您在console.log(datais)中看到的那样; 您将能够访问datais.data.weather中的weather属性。 试试这个
console.log("Weather::"+datais.data.weather);
您将能够访问weather属性。
进一步的天气属性是一个数组,它具有从今天到未来4天的天气值。您可以在循环中访问它们
for(i=0; i<datais.data.weather.length; i++){
console.log(datais.data.weather[i]);
}
不工作。。我正在检查json.parser.online.fr中的json数据……这是我的提琴。。。请检查一下数据。数据。天气如何?你不需要JSON。stringify angular为你做这个吗?@Michael..我需要stringyfy。。因为URL的输出格式不是JSON格式。。请检查一下我是否换了你的小提琴()你得到了完全符合你需要的控制台输出…工作起来很有魅力。。感谢您检查此更新无需执行json.stringifyJSON.stringify实际将json对象转换为字符串检查
for(i=0; i<datais.data.weather.length; i++){
console.log(datais.data.weather[i]);
}
console.log(datais.data.weather[0].date);
console.log(datais.data.weather[0].tempMaxC);
console.log(datais.data.weather[0].tempMinC);