title: how to get JSON objects from servlet?
body: <p>I am making a simple html servlet program where I need get JSON object from the servlet, and in html I am able to get the data, but how can i refer to each attribute?</p>
  
  <p>Here is the servlet <code>get</code> method</p>
  
  <pre><code>    PrintWriter out=response.getWriter();
      StringBuffer emps = new StringBuffer("{employees:[");
      emps.append("{fullname:\"Abhishek Raj Simon\"},{email:\"a@a.com\"}]");
      emps.append("}");
      out.println(emps);
  </code></pre>
  
  <p>JS to send</p>
  
  <pre><code>function getJson()
  {
   var url_action="/temp/jsontest";
   var form=document.forms["mainForm"];
   var splitOutput;
   var client; 
   var dataString;
  
   if (window.XMLHttpRequest){ 
       client=new XMLHttpRequest();
   } else {
       client=new ActiveXObject("Microsoft.XMLHTTP");
   }
  
   client.onreadystatechange=function(){
       if(client.readyState==4&amp;&amp;client.status==200)
       {
          var res=client.responseText;
          alert(res);     
          alert(client.responseText.employees.fullname); //DOES NOT WORK
          alert(client.responseText.employees.email); //DOES NOT WORK     
       }
   };
  
   client.open("GET",url_action,true);
   client.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   client.send();
  </code></pre>
  
  <p>and a simple form</p>
  
  <pre><code>&lt;form&gt;
      &lt;input type="button" value="get me some json" onclick="getJson();"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>When i click on the button, i get only 1 alert displaying <br/><code>{employees:[{fullname:"Abhishek Raj Simon"},{email:"a@a.com"}]}</code> How can i fetch <code>Abhishek Raj Simon</code> and <code>a@a.com</code> using fullname and email respectively?</p>
  
  <p><strong>Edited after reading post from Artem</strong></p>
  
  <p>my servlet</p>
  
  <pre><code>    Gson gson = new Gson( );
      List&lt;Employee&gt; employees = new ArrayList&lt;Employee&gt;();
      Employee emp=new Employee();
      emp.setFullname("Abhishek Raj Simon");
      emp.setEmail("a@a.com");
      employees.add(emp);
      response.setContentType( "application/json");
      out.println( gson.toJson( employees));
  </code></pre>
  
  <p>js part</p>
  
  <pre><code>var res=eval('(' + client.responseText + ')');
          alert(res);     
          alert(res.employees.fullname);
          alert(res.employees.email);
  </code></pre>