使用 Javascript 中的键循环遍历 JSON responseText 数组
使用 JavaScript。我正在尝试循环使用 JSON 编码的数组。以下是数组的示例:
{"test1":"some info","test2":"more info","test3":"more stuff"}
在每个循环内,我检查是否存在带有键名称的 DIV id。
<div id="test1"></div>
<div id="test2"></div>
<div id="test3"></div>
我正在使用 for() 循环,但无法让它工作。如果我删除 for() 循环,如果我只搜索 1 个 DIV id,它就可以正常工作。
for(var key in responseText)
这是脚本。有谁知道如何使用数组键作为 DIV id 的名称来循环responseText 中的数组?
<script>
function loadInfo() {
var req = new Request({
method: 'get',
url: 'getinfo.php,
noCache: true,
onRequest: function() {
for (var key in responseText) {
if (document.getElementById(key)) {
$(key).set('html', 'Loading');
}
}
},
onComplete: function(responseText, responseHtml) {
if (JSON.decode(responseText) != null) {
var data = JSON.decode(responseText);
for (var key in responseText) {
if (document.getElementById(key)) {
$(key).set('html', data[key]);
}
}
}
},
onFailure: function() {
for (var key in responseText) {
if (document.getElementById(key)) {
$(key).set('html', '-');
}
}
}
}).send();
}
window.addEvent('domready', function() {
loadInfo();
});
</script>
Using Javascript. I'm trying to loop through an array encoded with JSON. Here is a sample of the array:
{"test1":"some info","test2":"more info","test3":"more stuff"}
Inside each loop I am checking to see if a DIV id exists with the name of the keys.
<div id="test1"></div>
<div id="test2"></div>
<div id="test3"></div>
I'm using a for() loop, but I can't get it to work. If I remove the for() loop it works just fine if I search for only 1 DIV id.
for(var key in responseText)
Here is the script. Does anyone know how I can loop through the array from responseText using the array keys as the names of the DIV id's?
<script>
function loadInfo() {
var req = new Request({
method: 'get',
url: 'getinfo.php,
noCache: true,
onRequest: function() {
for (var key in responseText) {
if (document.getElementById(key)) {
$(key).set('html', 'Loading');
}
}
},
onComplete: function(responseText, responseHtml) {
if (JSON.decode(responseText) != null) {
var data = JSON.decode(responseText);
for (var key in responseText) {
if (document.getElementById(key)) {
$(key).set('html', data[key]);
}
}
}
},
onFailure: function() {
for (var key in responseText) {
if (document.getElementById(key)) {
$(key).set('html', '-');
}
}
}
}).send();
}
window.addEvent('domready', function() {
loadInfo();
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在迭代键之前,您必须解码 JSON。因此,您所说的:
将其替换为:
假设
此外,您的某些回调函数未指定
responseText
作为参数。如果您想为每个回调访问此内容,则必须显式包含responseText
作为参数。示例:应该是:
You have to decode the JSON before you iterate over the keys. So, where you say:
replace that with:
assuming
Also, some of your callback functions don't specify
responseText
as a parameter. If you want to access this for each callback, you have to explicitly includeresponseText
as a parameter. Example:should be:
我认为问题是您使用了错误的变量名称。
应读取
请注意,它读取的是
data
,而不是in
之后的responseText
。I think the problem is that you're using the wrong variable name.
Should read
Note that instead of
responseText
afterin
, it readsdata
.您确定不需要
JSON.parse
吗?这会将 JSON 响应解析为一个 javascript 对象,您可以使用for/in
对其进行操作。另外,您在
url:
之后缺少右引号Are you sure you don't want
JSON.parse
? This would parse the JSON response into a javascript object that you can use thefor/in
against.Also, you're missing a closing quotation mark after the
url: