[英]How do I pull JSON data from this API?
I'm reworking a weather APP I made using a different API. 我正在重新制作使用其他API制作的天气APP。 I've run into an issue where I can't pull anything from the JSON i get from said API. 我遇到了一个问题,我无法从我从上述API获得的JSON中提取任何内容。 Am I using the wrong notation or is my JSON not actually being passed into my JS script correctly? 我使用了错误的符号,还是我的JSON实际上没有正确传递到我的JS脚本中?
Code on my local machine 我的本地机器上的代码
$(document).ready(function () {
var long;
var lat;
var currentCity;
var currentState;
var country;
$.getJSON("https://crossorigin.me/http://ip-api.com/json", function (data_init) { //access RESTFUL geo location API & set lattitude.longitude
lat = data_init.lat;
long = data_init.lon;
currentCity = data_init.city;
currentState = data_init.region;
country = data_init.country;
$(".heading").append("<h2>" + currentCity + "," + currentState + "<br>" + country + "</h2>");
var api = "https://crossorigin.me/https://api.darksky.net/forecast/1246aa267663e22a4e428e4b20f0df5b/" + lat + "," + long;
//Access weather API
console.log(api);
console.log(currentCity);
console.log(currentState);
console.log(country);
$.getJSON(api, function (data) {
var iconCode = data.currently.icon; //get Icon from API related to current weather conditions
$(".message").append("<h4 id='tempData'>Current Temperature: " + data.currently.temp + "℃</h4>");
$(".message").append("<h4>Conditions: " + data.weather[0].main + "</h4>");
$("#reveal").on('click', function () { //click button
data.main.tempData //on click convert temperature to farenheight
});
$(".message").append("<img id='conditions' src=" + iconUrl + ">");
$("#tempData").hover(function () {
$(this).fadeToggle('slow', function () {});
});
console.log(data);
});
});
//$("#reveal").on("click", function(){
//});
});
I don't know what API you are using, but it looks like your callback is incomplete. 我不知道您使用的是什么API,但是您的回调似乎不完整。
Callbacks are usually written in this format: 回调通常以以下格式编写:
$.getJson("your url", function (err, data) { ... })
You probably forgot to handle err parameter on it. 您可能忘了处理err参数。
Answer: My syntax for accessing the JSON was incorrect. 答:我访问JSON的语法不正确。 I was trying to access the property 'Currently'.It should be... 我正在尝试访问``当前''属性,应该是...
var iconCode = data.currently.icon;
$(".message").append("<h4 id='tempData'>Current Temperature: "+data.currently.temperature+"℃</h4>");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.