如何循环遍历JavaScript中$.getJSON()方法返回的json onject

如何循环遍历JavaScript中$.getJSON()方法返回的json onject,javascript,json,api,weather-api,openweathermap,Javascript,Json,Api,Weather Api,Openweathermap,我不熟悉JavaScript和JQuery。我正在尝试使用OpenWeatherMapAPI获取天气数据。我正在尝试使用OpenWeatherMapAPI遍历$.getJSON()方法返回的对象 html代码: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>Weather Application</title>

我不熟悉JavaScript和JQuery。我正在尝试使用OpenWeatherMapAPI获取天气数据。我正在尝试使用OpenWeatherMapAPI遍历
$.getJSON()
方法返回的对象

html代码

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Weather Application</title>
    <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
    <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
        <script src="assets/js/script.js"></script>
   </head>

<body>
<header>
    <h1>Weather Forecast</h1>
</header>


<p id="location"></p>

<p id="json"></p>

<!--<form id="search-form">-->
    <!--<fieldset>-->
        <!--<input type="textbox" id="search" name="search" align="right" />-->
        <!--<button id="search-submit" align="right" value="Go" />-->
    <!--</fieldset>-->
<!--</form>-->

<div style="padding:16px;">
    Enter the name of  city : <input id="c1"type="text" value="Carbondale"/>
    <button id="button1" >Go</button>
</div>



<div>
   <p id="result"></p>
</div>

<div>
    <h1>Forecast Data</h1>
    Enter the name of  city : <input id="c2" type="text" value="....."/>
             Number of Days : <input id="days" type="text" value="1"/>
    <button id="button2">Go</button>

    <p id="forecast"></p>

</div>
</body>
</html>

气象应用
天气预报

输入城市名称: 去

预测数据 输入城市名称: 天数: 去

对应的JavaScript代码

$(document).ready(function ()
{
$("#button2").click(function () {
        var search_City2 = $("#c2").val();
        var days = $("#days").val();

        var count = Number(days);

        var search_url2 =  "http://api.openweathermap.org/data/2.5/forecast/daily?q="+search_City2+
        "&mode=json&units=metric&cnt="+days;

        $("#forecast").html(search_City2 + " "+count+" "+search_url2+"</br>");
        //document.getElementById("location").innerHTML = city+ " "+url;
        var mydate = new Date();


            $.getJSON(search_url2, function (result) {
                $.each(result,function(i,field){
                    $("#forecast").append(JSON.stringfy(field));
                });

                //for(i =0;i<count;i++) {
                    //$("#forecast").append("</br><b>" + i + " " + count +" " + " </b>");
                    //$("#forecast").append("</br>Temperature at Day :" + result.list[5].temp.day);
                   // $("#forecast").append("</br>Temperature at Morning :" + result.list[i].temp.morn);
                   // $("#forecast").append("</br>Temperature at Evening :" + result.list[i].temp.eve);
                   // $("#forecast").append("</br>Temperature at Night :" + result.list[i].temp.night);
                   // $("#forecast").append("</br>Max Temperature:" + result.list[i].temp.max);
                    //$("#forecast").append("</br>Min Temperature:" + result.list[i].temp.min);
                    //$("#forecast").append("</br>Humidity: " + result.list[i].humidity + "%");
                   // $("#forecast").append("</br>Weather Condition : " + result.list[i].weather[i].description);
                    //$("#forecast").append("</p></div>");
                    // $("#forecast").append("</br>Temperature :" + result.list[1].temp.day);
                    // $("#result").append("</br>Humidity :"+ result.list[0].main.humidity);
                    // $("#result").append("</br>Weather Condition :"+result.list[0].weather[0].description) ;
                //}
            });
    });
});
$(文档).ready(函数()
{
$(“#按钮2”)。单击(函数(){
var search_City2=$(“#c2”).val();
var days=$(“#days”).val();
var计数=数量(天);
var search_url2=”http://api.openweathermap.org/data/2.5/forecast/daily?q=“+搜索城市2+
“&mode=json&units=metric&cnt=“+days;
$(“#预测”).html(搜索城市2+“”+计数+“”+搜索城市2+”
”; //document.getElementById(“位置”).innerHTML=city+“”+url; var mydate=新日期(); $.getJSON(搜索\u url2,函数(结果){ $.each(结果、函数(i、字段){ $(“#预测”).append(JSON.stringfy(field)); });
//对于(i=0;i我不确定您想做什么,但下面是我对该问题的最佳猜测解决方案。它包含了一个
.forEach
循环,以循环查看响应中每天的预测,并保留您解析字段的方式

小提琴:

这里的错误:
JSON.stringfy(字段)->JSON.stringify(字段)

使用循环更新代码:

            $.getJSON(search_url2, function (result) {
                $.each(result,function(i,field){
                    $("#forecast").append(JSON.stringify(field));
                });

                result.list.forEach(function(forecast, i) {
                    $("#forecast").append("</br><b>" + i + " " + result.list.length +" " + " </b>");
                    $("#forecast").append("</br>Temperature at Day :" + forecast.temp.day);
                    $("#forecast").append("</br>Temperature at Morning :" + forecast.temp.morn);
                    $("#forecast").append("</br>Temperature at Evening :" + forecast.temp.eve);
                    $("#forecast").append("</br>Temperature at Night :" + forecast.temp.night);
                    $("#forecast").append("</br>Max Temperature:" + forecast.temp.max);
                    $("#forecast").append("</br>Min Temperature:" + forecast.temp.min);
                    $("#forecast").append("</br>Humidity: " + forecast.humidity + "%");
                    forecast.weather.forEach(function(weather) {
                        $("#forecast").append("</br>Weather Condition : " + weather.description);
                    });
                    $("#forecast").append("</p></div>");
                    $("#forecast").append("</br>Temperature :" + forecast.temp.day);
                    $("#result").append("</br>Humidity :"+ forecast.main.humidity);
                    $("#result").append("</br>Weather Condition :"+forecast.weather[0].description) ;
                });
            });
$.getJSON(搜索\u url2,函数(结果){
$.each(结果、函数(i、字段){
$(“#预测”).append(JSON.stringify(field));
});
result.list.forEach(函数(预测,i){
$(“#预测”).append(“
”+i+”+result.list.length++”); $(“#预测”).append(“
当天温度:”+forecast.temp.Day); $(“#预测”).append(“
上午温度:”+forecast.temp.morn); $(“#预测”).append(“
晚上的温度:”+forecast.temp.eve); $(“#预测”).append(“
夜间温度:”+forecast.temp.Night); $(“#预测”).append(“
最高温度:”+forecast.temp.Max); $(“#预测”).append(“
最低温度:”+forecast.temp.Min); $(“#预测”)。追加(“
湿度:”+forecast.湿度+“%”); forecast.weather.forEach(功能(天气){ $(“#预测”)。追加(“
天气状况:“+Weather.description”); }); $(“#预测”)。追加(“

”); $(“#预测”).append(“
温度:”+forecast.temp.day); $(“#结果”).append(“
湿度:”+forecast.main.湿度); $(“#结果”).append(“
天气状况:”+forecast.Weather[0]。description); }); });
您可以使用
$。每个(receivedData,callback);
非常感谢。它很有效。我还更改了for循环。实际上,我在索引时遇到了一个问题。该行应该是这样的:

    $("#forecast").append("</br>Weather Condition : " + result.list[i].weather[0].description)
$(“#forecast”).append(“
天气状况:”+result.list[i]。天气[0]。说明)
循环结构将是:

for (var i = 0; i < result.list.length; i++)
for(变量i=0;i
您的JavaScript代码是什么?