Decrypter Decrypter - 24 days ago 8
jQuery Question

Creating a json object in jsp and using it with JQuery

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.

url : "search.jsp",
data : "search=test",
success : function(html) {


for (int i = 0; i < size; i++) {

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
to jquery then let jquery parse the objects

Any help is appreciated :)


Here's an example you may take a look at. Basically your JSP page might look like this:

<%@page contentType="text/html; charset=UTF-8"%>
<%@page import="org.json.simple.JSONObject"%>
    JSONObject json = new JSONObject();
    json.put("title", "TITLE_TEST");
    json.put("link", "LINK_TEST");

and on the client:

    url : 'search.jsp',
    data : { search: 'test' },
    dataType: 'json',
    success : function(json) {

And here are even more examples.