[英]parse JSON weather object from Open Weather Map API using AFNetworking
[英]Unable to pull one item of data from the Open Weather Map api using JSON
所有。
這是一個測試,看看我是否可以連接到開放天氣地圖api並成功顯示它的數據項(現在我只是想獲得給定緯度和經度的城市名稱)。
順便說一下,我在CodePen筆中這樣做。
問題是頁面上沒有任何內容 - 我根本沒有城市名稱。
這是什么遺失? (我掩蓋了我的api鍵)
<html><body>
<div class="Text-center">
<h1> Local Weather</h1>
<h3>Front End Dev Project</h3>
<ul class ="list-unstyled">
<li class="btn btn-default" id="city"></li>
</ul>
<div>
</body></html>
<script>
$(document).ready(function(){
var long= -77.0506895;
var lat = 38.8925157;
var api = 'http://api.openweathermap.org/data/2.5/weatherlat='+lat+'&lon'+long+'=&appid=(HIDDEN)';
$.getJSON(api, function(data){
var city = data.name;
$("#city").html(city);
});
});
</script>
這是正確的網址,請更正您的網址
http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid= {AppKey}
以及下面的回復
{
"coord":{
"lon":138.93,
"lat":34.97
},
"weather":[
{
"id":800,
"main":"Clear",
"description":"clear sky",
"icon":"01n"
}
],
"base":"cmc stations",
"main":{
"temp":297.374,
"pressure":1018.36,
"humidity":93,
"temp_min":297.374,
"temp_max":297.374,
"sea_level":1027.9,
"grnd_level":1018.36
},
"wind":{
"speed":7.46,
"deg":243.504
},
"clouds":{
"all":0
},
"dt":1467816873,
"sys":{
"message":0.0045,
"country":"JP",
"sunrise":1467747404,
"sunset":1467799281
},
"id":1851632,
"name":"Shuzenji",
"cod":200
}
好的,以下工作在CodePen外面。 如果我在CodePen中使用它,它會以某種方式阻止JSON調用。 如果我在Chrome中使用NotePad編寫的版本運行它,它會返回“Shuzenji”就好了。
但這是另一個問題的問題。
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js" ></script>
<script type="text/javascript">
$(document).ready(function(){
var api = 'http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=52481ca8a499564783f5f082863acac1';
$.getJSON(api, function(data){
var city = data.name;
$("#city").html(city);
});
});
</script>
</head>
<body>
<div class="Text-center">
<h1> Local Weather</h1>
<h3>Front End Dev Project</h3>
<ul class ="list-unstyled">
<li class="btn btn-default" id="city"></li>
</ul>
<div>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.