简体   繁体   English

无法从带有 jquery 的 API 获取天气值

[英]Can not get weather values from the API with jquery

I try to get the weather values from the weather API and i can not take the city name and the other properties.我尝试从天气 API 获取天气值,但我无法获取城市名称和其他属性。 When i try to push them in the array items can not project it to HTML page当我尝试将它们推入数组项目时,无法将其投影到 HTML 页面

See the code below:请看下面的代码:

var url = "https://api.weatherbit.io/v2.0/forecast/daily?city=london,gb,&key=f0e406af6ebf4937862f9f50ae78125b";

function getWeather(){
$.getJSON( url, function( data ) {
  var items = [];
  $.each( data, function( key, val ) {
    var weather = {
      cityName:val.city_name,
      longtitude:val.lon,
      timezone:val.timezone,
      latitude:val.lat,
      countryState:val._state
    }


    items.push("<li>"+weather+"</li>");
  });

  $( "<ul/>", {
    "class": "my-new-list",
    html: items.join( "" )
  }).appendTo( "body" );
});
}

You can access those properties like in the demo.您可以像在演示中一样访问这些属性。

Here is working Demo: https://jsfiddle.net/usmanmunir/acghx45z/19/这是工作演示: https://jsfiddle.net/usmanmunir/acghx45z/19/

 var url = "https://api.weatherbit.io/v2.0/forecast/daily?city=london,gb,&key=f0e406af6ebf4937862f9f50ae78125b"; function getWeather(){ $.getJSON( url, function( data ) { var items = []; let newData = {} $.each(data, function( key, val ) { if (key;= 'data') { newData[key] = data[key] } }). $,each(newData, function( key. val ) { items:push("<li>"+key+"; "+val+"</li>"); }), $( "<ul/>": {"class", "my-new-list":html. items});appendTo( "body" ); }); } getWeather()
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Here is a simplified version:这是一个简化版本:


var url = "https://api.weatherbit.io/v2.0/forecast/daily?city=london,gb,&key=f0e406af6ebf4937862f9f50ae78125b";

function getWeather(){
$.getJSON( url, function( data ) {
  var items = [];

  items.push("<li>"+data.city_name+"</li>");
  items.push("<li>"+data.lon+"</li>");
  items.push("<li>"+data.timezone+"</li>");
  items.push("<li>"+data.lat+"</li>");
  items.push("<li>"+data.state_code+"</li>");


  $( "<ul>", {
    "class": "my-new-list",
    html: items.join( "" )
  }).appendTo( "body" );
});
}

getWeather();

You don't need a loop.你不需要循环。 In fact, from what you're trying to do, a loop makes no sense if all you're trying to get are these specific values.事实上,从你想要做的事情来看,如果你想要得到的只是这些特定的值,那么循环是没有意义的。

In your code, you loop over every key in the data object and create a new object with every iteration that gets then pushed to your items array.在您的代码中,您循环遍历数据 object 中的每个键,并在每次迭代中创建一个新的 object,然后将其推送到您的 items 数组。 You are also trying to access undefined values.您还试图访问未定义的值。

The answer from above isn't correct since it goes through every key in the object which itself can be an object and results in broken output like [object]. 上面的答案是不正确的,因为它遍历了 object 中的每个键,它本身可以是 object 并导致 output 像 [object] 一样损坏。

Have a look at the documentation over at mozilla: Javascript - Working with objects查看 mozilla 上的文档: Javascript - 使用对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM