使用 Javascript 中的键循环遍历 JSON responseText 数组

发布于 2024-10-01 18:46:04 字数 1399 浏览 0 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独﹏钓一江月 2024-10-08 18:46:04

在迭代键之前,您必须解码 JSON。因此,您所说的:

for(var key in responseText) {

将其替换为:

for(var key in data) {

假设

 var data = JSON.decode(responseText);

此外,您的某些回调函数未指定 responseText 作为参数。如果您想为每个回调访问此内容,则必须显式包含 responseText 作为参数。示例:

onRequest: function(){

应该是:

onRequest: function(responseText){

You have to decode the JSON before you iterate over the keys. So, where you say:

for(var key in responseText) {

replace that with:

for(var key in data) {

assuming

 var data = JSON.decode(responseText);

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 include responseText as a parameter. Example:

onRequest: function(){

should be:

onRequest: function(responseText){
吹梦到西洲 2024-10-08 18:46:04

我认为问题是您使用了错误的变量名称。

var data = JSON.decode(responseText);

for(var key in responseText) {

应读取

var data = JSON.decode(responseText);

for(var key in data) {

请注意,它读取的是 data,而不是 in 之后的 responseText

I think the problem is that you're using the wrong variable name.

var data = JSON.decode(responseText);

for(var key in responseText) {

Should read

var data = JSON.decode(responseText);

for(var key in data) {

Note that instead of responseText after in, it reads data.

小傻瓜 2024-10-08 18:46:04

您确定不需要 JSON.parse 吗?这会将 JSON 响应解析为一个 javascript 对象,您可以使用 for/in 对其进行操作。

var data = JSON.parse(responseText);

另外,您在 url: 之后缺少右引号

url:'getinfo.php',  // Closed the quote

Are you sure you don't want JSON.parse? This would parse the JSON response into a javascript object that you can use the for/in against.

var data = JSON.parse(responseText);

Also, you're missing a closing quotation mark after the url:

url:'getinfo.php',  // Closed the quote
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文