在下面的代码中,我尝试迭代 JSON 对象字符串。但是,我没有得到所需的输出。我在网页上的输出如下所示:-
+item.温度++item.温度++item.温度++item.温度+
输出温度的警报效果很好。我尝试通过迭代 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>
最佳答案
请勿在 .append()
部分的 $("body").append("+item.Temperature+");
中使用引号。
应该是
$(document.body).append(item.temperature);
像您一样用引号编写该表达式,只需一遍又一遍地添加一个字符串
。 Java//Ecmascript 将任何带引号的内容解释为字符串文字。
请注意,我还用 document.body
替换了 "body"
。这主要是出于性能//访问原因,因此有更好的业力。
关于javascript - 迭代 JSON 对象时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3230550/