Javascript 迭代JSON对象时出现问题

Javascript 迭代JSON对象时出现问题,javascript,jquery,json,iterator,Javascript,Jquery,Json,Iterator,在下面的代码中,我尝试迭代JSON对象字符串。但是,我没有得到所需的输出。我在网页上的输出类似于:- +item.temperature++item.temperature++item.temperature++item.temperature+ 输出温度的警报工作正常。我尝试通过迭代JSON对象字符串来访问值的部分似乎不起作用。谁能帮我修一下这个吗 代码 <body> <script> $.getJSON('http://ws.geonames.org/

在下面的代码中,我尝试迭代JSON对象字符串。但是,我没有得到所需的输出。我在网页上的输出类似于:-

+item.temperature++item.temperature++item.temperature++item.temperature+

输出温度的警报工作正常。我尝试通过迭代JSON对象字符串来访问值的部分似乎不起作用。谁能帮我修一下这个吗

代码

<body>
    <script>
    $.getJSON('http://ws.geonames.org/weatherJSON?north=90&south=-9.9&east=-22.4&west=55.2',
      function(data) {
        $.each(data.weatherObservations, function(i, item) {
          $("body").append("+item.temperature+");
          if (-i == 3-) return false;
        });
        alert(data.weatherObservations[0].temperature);
      });
    </script>
</body>

$.getJSON('http://ws.geonames.org/weatherJSON?north=90&south=-9.9东部=-22.4西部=55.2',
功能(数据){
$。每个(数据。天气观测,功能(i,项目){
$(“正文”)。附加(“+项目温度+”);
如果(-i==3-),则返回false;
});
警报(数据。天气观测[0]。温度);
});

不要在
$(“body”)中使用引号。追加(“+item.temperature+”)
.append()部分中

应该是

$(document.body).append(item.temperature);
像您那样用引号编写表达式,只需反复添加一个
字符串。Java//Ecmascript将任何带引号的内容解释为字符串文字


请注意,我还将
“body”
替换为
document.body
。这主要是因为性能//访问原因,所以更好的业力。

不要在
$(“body”)中使用引号。追加(“+item.temperature+”)
.append()部分中

应该是

$(document.body).append(item.temperature);
像您那样用引号编写表达式,只需反复添加一个
字符串。Java//Ecmascript将任何带引号的内容解释为字符串文字


请注意,我还将
“body”
替换为
document.body
。这主要是由于性能//访问原因,因此会带来更好的业力。

您的代码正在迭代,但您正在添加“+item.temperature+”,您不想执行以下操作吗

$("body").append("Temp is " + item.temperature);


您的代码正在迭代,但您正在添加“+item.temperature+”,您不想执行以下操作吗

$("body").append("Temp is " + item.temperature);

“+item.temperature+”
表示一个字符串,它是
“+item.temperature+”

“pre”+item.temperature+“post”
将变量连接到字符串

$.getJSON('http://ws.geonames.org/weatherJSON?north=90&south=-9.9&east=-22.4&west=55.2', 
  function (data) {
    $.each(data.weatherObservations, function (i, item) {
        $("body").append(item.temperature + ",");
        if (i == 3) return false;
    });
    alert(data.weatherObservations[0].temperature);
});
“+item.temperature+”
表示一个字符串,它是
“+item.temperature+”

“pre”+item.temperature+“post”
将变量连接到字符串

$.getJSON('http://ws.geonames.org/weatherJSON?north=90&south=-9.9&east=-22.4&west=55.2', 
  function (data) {
    $.each(data.weatherObservations, function (i, item) {
        $("body").append(item.temperature + ",");
        if (i == 3) return false;
    });
    alert(data.weatherObservations[0].temperature);
});