title: How to parse a JSON object from ajax call in Java Script
body: <p>I am making an Ajax call in my JSP which is recieving a JSON response.</p>
  
  <p>Running <code>alert('Ajax Response ' + respArr);</code> gives the following screen:</p>
  
  <p><img src="https://i.stack.imgur.com/v2MSC.jpg" alt="Screenshot of Alert message"></p>
  
  <p>The Java server-side code:</p>
  
  <pre><code>public void doGet(HttpServletRequest request, HttpServletResponse res) throws IOException, ServletException {
      try {
          String fromDate = request.getParameter("drFrom");
          String toDate = request.getParameter("drTo");
          JSONArray jsonArray = chartData.getCCSBJson(fromDate, toDate);
          res.setContentType("application/json");
          res.getWriter().write(jsonArray.toString());
      }
  </code></pre>
  
  <p>The JavaScript:</p>
  
  <pre><code>if (xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200) {
      if(xmlhttp.responseText != null) {
          var respArr = xmlhttp.responseText;
          var jsonData = eval("(" + respArr + ")");
          alert('JSON Chart ' + jsonData); // The line from above
          var obj = JSON.parse(xmlhttp.responseText);
          alert('JSON Parse' + obj);
  </code></pre>
  
  <p>The JSON that is being returned:</p>
  
  <pre><code>[
    {
      "chart":{
        "caption":"",
        "exportDataFormattedVal":"1",
        "numberPrefix":"",
        "showexportdatamenuitem":"1",
        "xAxisName":"Bureau usage",
        "yAxisName":"count"
      },
      "data":[
        {
          "label":"SB AutoDecison",
          "value":"0"
        },
        {
          "label":"CC AutoDecison",
          "value":"0"
        },
        {
          "label":"CC Judgemental",
          "value":"0"
        },
        {
          "label":"SB Judgemental",
          "value":"0"
        }
      ]
    }
  ]
  </code></pre>
  
  <p>The alerts result:</p>
  
  <pre><code>alert('JSON Chart ' + jsonData) // JSON Chart[object Object]
  alert('JSON Parse ' + obj);     // JSON parse[object Object]
  </code></pre>
  
  <p>What I want is to parse the object and generate an Excel sheet out of the content.</p>
  
  <p>When I try to loop:</p>
  
  <pre><code>var jqueryData = jQuery.parseJSON(respArr);
  var obj = JSON.parse(xmlhttp.responseText);
  for (var i in obj) {
      alert('For loop string' + obj[i]);
  }
  </code></pre>
  
  <p>It throws some 7 or 8 alerts with JavaScript code </p>
  
  <pre><code>for (i = 0; i &lt; 5; i++) {
      alert(i + ' of respArr ' + respArr[i]);
  }
  </code></pre>
  
  <p>Gives letter after letter of the JSON: <code>[</code>, <code>{</code>, <code>"</code>, <code>c</code>, <code>h</code>, and so on for each iteration of the loop.</p>
  
  <p>Can I not just traverse the JSON like <code>respArr[0].data</code> or <code>respArr[0].chart</code>?</p>