通过使用带有JavaScript中的密钥的JSON responseText的阵列循环 [英] Loop through a JSON responseText array using the keys with Javascript

查看:67
本文介绍了通过使用带有JavaScript中的密钥的JSON responseText的阵列循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JavaScript。我通过一个数组带JSON codeD试图循环。这里是该阵列的一个示例:

  {测试1:一些信息,测试2:更多信息,TEST3:更多的东西}

每个循环我检查,看是否有DIV ID与键的名称存在里面。

 < D​​IV ID =测试1>< / DIV>
< D​​IV ID =test2的>< / DIV>
< D​​IV ID =TEST3>< / DIV>

我使用一个for()循环,但我不能得到它的工作。如果我删除了()循环,如果我只搜索1 DIV ID的工作就好了。

 为(以responseText的VAR键)

下面是脚本。有谁知道我可以通过从responseText的数组循环使用数组键作为DIV ID的名称?

 <脚本>
功能loadInfo(){
VAR REQ =新的请求({
    方法:'得到',
    网址:'getinfo.php,
    NOCACHE:真实,
    onRequest:功能(){        对(在responseText的VAR键){            如果(的document.getElementById(键)){                $(键).SET(HTML,正在载入);            }        }    },
    的onComplete:功能(responseText的,responseHtml){
        如果(JSON.de code(responseText的)!= NULL){
            VAR数据= JSON.de code(responseText的);            对(在responseText的VAR键){                如果(的document.getElementById(键)){                    $(键).SET(HTML,数据[关键]);                }            }
        }
    },
    onFailure处:函数(){        对(在responseText的VAR键){            如果(的document.getElementById(键)){                                 $(键).SET('HTML',' - ');
                            }        }    }
})。发送();
}
window.addEvent('domready中',函数(){
loadInfo();
});
< / SCRIPT>


解决方案

您有您遍历密钥之前取消code中的JSON。所以,在这里你说:

 为(以responseText的VAR键){

替换成:

 的(数据VAR键){

假设

  VAR数据= JSON.de code(responseText的);

另外,你的一些回调函数不指定的responseText 作为参数。如果你想访问此为每个回调,你必须明确地包含的responseText 作为参数。例如:

  onRequest:功能(){

应该是:

  onRequest:功能(responseText的){

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>

解决方案

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){

这篇关于通过使用带有JavaScript中的密钥的JSON responseText的阵列循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆