title: Loop through a JSON responseText array using the keys with Javascript
body: <p>Using Javascript. I'm trying to loop through an array encoded with JSON. Here is a sample of the array:</p>
  
  <pre><code>{"test1":"some info","test2":"more info","test3":"more stuff"}
  </code></pre>
  
  <p>Inside each loop I am checking to see if a DIV id exists with the name of the keys.</p>
  
  <pre><code>&lt;div id="test1"&gt;&lt;/div&gt;
  &lt;div id="test2"&gt;&lt;/div&gt;
  &lt;div id="test3"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>for(var key in responseText)
  </code></pre>
  
  <p>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?</p>
  
  <pre><code>&lt;script&gt;
  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();
  });    
  &lt;/script&gt;
  </code></pre>