I've created a JSP application, which gets results based on a user search (using lucene). I store the results in a Bean.
I'm also using Jquery Ajax to display the results.
$.ajax({
url : "search.jsp",
data : "search=test",
success : function(html) {
("#search_results").hide().html(html).fadeIn(1500);
}
});
search.jsp
for (int i = 0; i < size; i++) {
out.println(searchResult.get(i).getHTML());
}
This is working fine, however I want to change it so it returns a JSON object to JQuery and then let JQuery parse the objects and display the results
I am not sure how to do this as I'm new to JSON objects and JSP. I could possibly do something like
JSONObject json = new JSONObject();
json.put("title", "TITLE_TEST");
json.put("link", "LINK_TEST");
but I dont know how to return json
to jquery then let jquery parse the objects
Any help is appreciated :)
This worked for me:
I used it to feed an easyui-datagrid.
response.getWriter().write(json)
worked, butout.println(json)
did not though he didn't throw any exceptions. Also the inner quotes must be double as well, so it becomes necessary to mask them with `\".Pretty simple approach would be to use taglib - json something like this :
Then you can use json tags to create it out of list:
Above jsp when executes will O/P following :
Thats all folks!
Here's an example you may take a look at. Basically your JSP page might look like this:
and on the client:
And here are even more examples.
Ultimately its being trasnferred over http. So, creating a json object wont do much help.
I am not a java expert but you can create a simple string which matches with json structure and then parse it on client side.
Like
This will do the trick.
Edit: by seeing Darin's answer,
Include this on you java code,