繁体   English   中英

如何从 API 检索数据并将其转换为网页?

[英]How do I retrieve and transpose data to a webpage from an API?

我在从 API 访问数据时遇到问题。 这是我的带有假访问代码的代码。

$(function () {

    var $data = ('#data');
    $.ajax({
        type: 'GET',
        url: 'http://api.openweathermap.org/data/2.5/weather?lat=47.6062&lon=-122.3321&units=imperial&appid=a0bfe75fbff13d4040af7eb66d1d82b5',
        success: function (data) {
            $.each(data, function (i, item) {
                $data.append('<li>Temp: ' + item.main['temp'] + '</li>');
            });
        }
    });
    
});

我收到一条错误消息,指出“未捕获的类型错误:无法读取未定义的属性‘temp’”这是 API。

{
    "coord": {
        "lon": -122.3321,
        "lat": 47.6062
    },
    "weather": [
        {
            "id": 721,
            "main": "Haze",
            "description": "haze",
            "icon": "50d"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 61.36,
        "feels_like": 61.2,
        "temp_min": 55.49,
        "temp_max": 66.31,
        "pressure": 1022,
        "humidity": 85
    },
    "visibility": 10000,
    "wind": {
        "speed": 1.01,
        "deg": 319,
        "gust": 5.01
    },
    "clouds": {
        "all": 1
    },
    "dt": 1627137674,
    "sys": {
        "type": 2,
        "id": 2004026,
        "country": "US",
        "sunrise": 1627130239,
        "sunset": 1627185243
    },
    "timezone": -25200,
    "id": 5809844,
    "name": "Seattle",
    "cod": 200
}

我希望能够访问 temp 和 description 并将它们附加或添加到我的页面。 不需要是一个列表,但我想在 css 中设置它的样式。 随意更改任何代码。

{"coord":{"lon":-122.33,"lat":47.61},"weather":[{"id":721,"main":"Haze","description":"haze","icon":"50d"}],"base":"stations","main":{"temp":64.09,"feels_like":63.91,"temp_min":57.43,"temp_max":69.62,"pressure":1022,"humidity":79},"visibility":10000,"wind":{"speed":1.99,"deg":325,"gust":3},"clouds":{"all":1},"dt":1627141350,"sys":{"type":2,"id":2004026,"country":"US","sunrise":1627130238,"sunset":1627185243},"timezone":-25200,"id":5809844,"name":"Seattle","cod":200}```

要访问temp您不需要使用.each循环,您可以直接访问它们,即: data.main.temp和访问天气 -> 描述,您可以使用.each循环并在每个循环内使用item.description

演示代码

 var $data = ('#data'); //dummy json var data = { "coord": { "lon": -122.33, "lat": 47.61 }, "weather": [{ "id": 721, "main": "Haze", "description": "haze", "icon": "50d" }], "base": "stations", "main": { "temp": 64.09, "feels_like": 63.91, "temp_min": 57.43, "temp_max": 69.62, "pressure": 1022, "humidity": 79 }, "visibility": 10000, "wind": { "speed": 1.99, "deg": 325, "gust": 3 }, "clouds": { "all": 1 }, "dt": 1627141350, "sys": { "type": 2, "id": 2004026, "country": "US", "sunrise": 1627130238, "sunset": 1627185243 }, "timezone": -25200, "id": 5809844, "name": "Seattle", "cod": 200 } /* $.ajax({ //your codes.... */ console.log(data.main.temp) //use it like this... $.each(data.weather, function(i, item) { console.log(item.description) //get description like this.. }) /* } });*/
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>

暂无
暂无

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

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