简体   繁体   English

JavaScript请求JSON信息。 返回错误的结果

[英]JavaScript requesting JSON info. returns incorrect result

I'm quite new to JavaScript and keep having problems with importing JSONs. 我对JavaScript很陌生,并且在导入JSON时一直遇到问题。 I'm making something similar to the alarm seen in iron man, with Jarvis greeting the person. 我正在做与钢铁侠中看到的警报类似的事情,贾维斯向他打招呼。 When I try to import the weather through the OpenWeatherMap API, I get random results like a temperature of 0... I just need to define 4 variables, one with the temperature, one with the wind speed, one with the general description (data.weather.main) , and one with the humidity. 当我尝试通过OpenWeatherMap API导入天气时,会得到随机的结果,例如温度为0 ...我只需要定义4个变量,一个与温度有关,一个与风速有关,一个与一般说明(data.weather.main) ,然后再加上湿度。 If someone could tell me what I'm doing wrong, and/or show me an example of how to do it for one of the variables, I would be grateful. 如果有人可以告诉我我做错了什么,和/或向我展示如何针对其中一个变量进行操作的示例,我将不胜感激。 Thanks in advance 提前致谢

JSON: JSON:

{ "coord": { "lon": -71.37, "lat": 42.24 }, "weather": [{ "id": 802, "main": "Clouds", "description": "scattered clouds", "icon": "03n" }], "base": "stations", "main": { "temp": 63.55, "pressure": 1014, "humidity": 77, "temp_min": 62.6, "temp_max": 66.2 }, "visibility": 16093, "wind": { "speed": 4.7, "deg": 80 }, "clouds": { "all": 40 }, "dt": 1500871860, "sys": { "type": 1, "id": 2390, "message": 0.0161, "country": "US", "sunrise": 1500888653, "sunset": 1500941553 }, "id": 4950790, "name": "Sherborn", "cod": 200 }

CODE: 码:

<html>
<title>Good Morning</title>

<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>

<body>
    <script src="http://code.responsivevoice.org/responsivevoice.js"></script>
    <script>
        /*
                function setup() {
                $.getJSON('http://api.openweathermap.org/data/2.5/weather?q={Sherborn}&APPID=355c20fb396a58c1c25e0c341c9035ed&units=imperial', function(Data) {
               console.log (Data.weather.main);
                if (Data.weather.main = 'Clouds') {
                window.WeatherStatus = "Cloudy"
                }})} */
        $.ajax({
            url: 'http://api.openweathermap.org/data/2.5/weather?q={Sherborn}&APPID=355c20fb396a58c1c25e0c341c9035ed&units=imperial',
            datatype: 'json',
            type: 'get',
            cache: false,
            success: function(Data) {
                $(Data.main).each(function(temp, wind) {
                    console.log(temp);
                    console.log(wind)
                });
            }
        });
    </script>
</body>

</html>

change your return success to this: 将您的退货成功更改为:

success: function(Data) {
          console.log(Data.weather[0].main);//show weather
          console.log(Data.main.temp);//show temp
          console.log(Data.main.humidity);//show humidity
          console.log(Data.wind.speed);//show speed
        }

After AJAX returns success do 在AJAX返回成功后,请执行

console.log(Data.main.temp); 

//and //和

console.log(Data.wind.speed); 

you should see correct values 您应该看到正确的值

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

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