Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5943151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:21:14+00:00 2026-05-22T16:21:14+00:00

I am making a simple html servlet program where I need get JSON object

  • 0

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?

Here is the servlet get method

    PrintWriter out=response.getWriter();
    StringBuffer emps = new StringBuffer("{employees:[");
    emps.append("{fullname:\"Abhishek Raj Simon\"},{email:\"a@a.com\"}]");
    emps.append("}");
    out.println(emps);

JS to send

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&&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();

and a simple form

<form>
    <input type="button" value="get me some json" onclick="getJson();">
</form>

When i click on the button, i get only 1 alert displaying
{employees:[{fullname:"Abhishek Raj Simon"},{email:"a@a.com"}]} How can i fetch Abhishek Raj Simon and a@a.com using fullname and email respectively?

Edited after reading post from Artem

my servlet

    Gson gson = new Gson( );
    List<Employee> employees = new ArrayList<Employee>();
    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));

js part

var res=eval('(' + client.responseText + ')');
        alert(res);     
        alert(res.employees.fullname);
        alert(res.employees.email);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:21:15+00:00Added an answer on May 22, 2026 at 4:21 pm

    I think you should slightly change the JSON that you send form the servlet: {employees:[{fullname:"Abhishek Raj Simon", email:"a@a.com"}]} would work a bit better in that context.

    I’d recommend jQuery as pap has also advised. The code would be:

    $.getJSON('/temp/jsontest', function(data) {
       var items = [];
       for (employee in data.employees) {
           items.push(employee.fullname + '<' + employee.email + '>');
       }
       alert('Employees: ' + items.join(', '));
    });
    

    In my opinion it is lot simpler and easier to understand than dealing with raw XHR. The jQuery $.getJSON will do GET for you, and then evaluate the JSON response so the function is presented with nice JSON representation of your data that is easy to manipulate.

    EDIT:

    After interesting discussion here is some more improvement you could introduce in order to replace the low-level code with proper JQuery-based implementation.

    <script>
      $(document).ready(function() {
        $("#json-button").click(function() {
          $.getJSON('/temp/jsontest', function(data) {
            var items = [];
              for (employee in data.employees) {
                items.push(employee.fullname + '<' + employee.email + '>');
              }
              alert('Employees: ' + items.join(', '));
          });
        });
      });
    </script>
    
    <form>
        <input id="json-button" type="button" value="get me some json">
    </form>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a very simple html webpage consisting only of text. How can I
I'm making a simple jQuery navigation system but I am far from expert at
I'm making a simple tool that will get a string of MySQL commands and
I am making use of simplehtmldom which has this funciton: // get html dom
Somewhat related to my earlier question. I'm making a simple html parser to play
I'm making a simple template system. It's annoying to not to be able to
I am making a simple registration form and cannot get one of the select
Ok so I'm making a simple web app using the technologies from the topic,
So, I'm making a simple clock that just counts time from a static starting
I'm making simple webcam program using OpenCV 2.3 and got stuck by the compile

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.