0

Here is my code :

Display.jsp:

<%
response.setContentType("application/json");
JSONObject json = new JSONObject();
Employee employee = new Employee("RAM","ram@gmail.com");
json.put("employee",employee);
out.println(json);
%>

how can i parse employee name and email using js,

i want to set those values into a table

Any ideas...

Ram72119
  • 107
  • 18
  • possible duplicate of [Reading a JSP variable from JavaScript](http://stackoverflow.com/questions/4803906/reading-a-jsp-variable-from-javascript) – ivan.sim Oct 26 '14 at 07:30
  • here i'm not going to render my jsp variable to the view directly..i just give my jsp json variable to javascript...please take a clear look... – Ram72119 Oct 26 '14 at 08:31
  • The accepted answer in that post is not just about rendering jsp variables to view. Have you tried something like `var jsvariable="<%= json %>";`? – ivan.sim Oct 26 '14 at 08:37
  • Are you using ajax call? – Pradeep Kr Kaushal Oct 26 '14 at 08:40
  • i'm not using any ajax call at this time....to communicate with jsp and js in between some xml/json required thats why i prefer json – Ram72119 Oct 29 '14 at 06:19

2 Answers2

0

You can traverse thorough the object in the javascript to print its key and value ,

function inside_obj(o) {
    var Otype = typeof o 
    if (Otype == "object") {
        for (var key in o) {
            print("key: ", key)
            inside_obj(o[key])
        }
    } else {
        print(o)
    }
}

Learn More. .

Santhosh
  • 8,181
  • 4
  • 29
  • 56
0

Js runs on the client-side, whereas JSP - on the server side. Therefore, if you want to parse your JSP output HTML on the client side, you'll need to select this output (using jQuery is the most convenient way. Make sure you output it in a HTML tag with an ID you can use in jQuery selector). Then, use JSON.parse() to parse the json.

Also, why do you need JS to process your JSON? Can't you do that on the server side? Unless you're using AJAX, I can hardly think of a reason not to completely process your model on the server side.