简体   繁体   中英

Iterate over JSON object in from AJAX success

I have created below JSON in my servlet and sending Json back to ajax call in response.

I want to display Employee list in table. Please help me to retrieve value from JSON.

Thanks

{
  "First Name": "Last Name",
  "EmployeeList": [
    {
      "Ram": "Kumar"
    },
    {
      "Varun": "Kuamr"
    }
  ]
}

Below is my JSP COde.

$(document).ready(function() {

    $("#JsonStart").click(function(e) {
        e.preventDefault();

        alert("Hi");
        $.ajax({
            url: "/bin/getEmployee",
            method: "GET",
            success: function(myresponse) {
                alert("Inside Ajax" + myresponse);
                //    $("#ajaxResponse").text(myresponse);
                $("#ajaxResponse").html("");
                $("#ajaxResponse").append("<b>My Ajax Response :</b>" + myresponse);
                $.each(myresponse, function(key, value) {
                    alert("Value :" + value);
                    alert("Key :" + key);

                });

                console.log("success");
            },
            failure: function(myresponse) {
                CQ.Notification.notifyFromResponse(myresponse);
            }

        });

    });

});


<form>
    <div>
        <button type="button" id="JsonStart" name="Shareitem">TestJ</button>
    </div>
</form>
<div id="anotherSection">
    <fieldset>
        <legend>Response from jQuery Ajax Request</legend>
        <div id="ajaxResponse"></div>
    </fieldset>
</div>

Below is my servlet code :

public class EmployeeInfoServlet extends SlingSafeMethodsServlet {
     @Override
        protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException  {

                 JSONObject employeeJson = new JSONObject();
                 try {
                    employeeJson.put("EmployeeFirst", "Employee Last");
                        JSONArray empJsonArray = new JSONArray();
                            JSONObject Employee1=new JSONObject();
                            Employee1.put("Ram", "Kumar");
                            empJsonArray.put(Employee1);
                            JSONObject Employee2=new JSONObject();
                            Employee2.put("Varun", "Kuamr");
                            empJsonArray.put(Employee2);
                        employeeJson.put("EmployeeList", empJsonArray);

                        System.out.println("Employee JSON"+employeeJson.toString());



                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                    response.setContentType("application/json");
                    response.setCharacterEncoding("UTF-8");
                    response.getWriter().write(employeeJson.toString());


     }

}

IMO your json is not structured correctly:

{
  "EmployeeList": [
    {
      "name" : "Ram",
      "surname" : "Kumar"
    },
    {
      "name" : "Varun",
      "surname" : "Kuamr"
    }
  ]
}

After this, you can read/write json objects in a regular way:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM