Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am building web application using jsp and servlet, I send ajax request from jsp and I want to return two json objects from servlet. I tried to do the following but the code did not work.

// in jquery I wrote this code

        var id = $(this).attr('id');

        var paramenters = {"param":id};

        $.getJSON("MyServlet", paramenters, function (data1,data2){

            $("h3#name").text(data1["name"]);

            $("span#level").text(data1["level"]);

            $("span#college").text(data2["college"]);

            $("span#department").text(data2["department"]);

        });

// in the servlet I wrote this code

    String json1 = new Gson().toJson(object1);

    String json2 = new Gson().toJson(object2);

    response.setContentType("application/json");

    response.setCharacterEncoding("utf-8");

    response.getWriter().write(json1);

    response.getWriter().write(json2);

can someone help me???

share|improve this question
add comment (requires an account with 50 reputation)

4 Answers

up vote 11 down vote accepted

You should do it like this:

Server side:

String json1 = new Gson().toJson(object1); 
String json2 = new Gson().toJson(object2); 
response.setContentType("application/json"); 
response.setCharacterEncoding("utf-8"); 
String bothJson = "["+json1+","+json2+"]"; //Put both objects in an array of 2 elements
response.getWriter().write(bothJson);

Client side:

$.getJSON("MyServlet", paramenters, function (data){ 
   var data1=data[0], data2=data[1]; //We get both data1 and data2 from the array
   $("h3#name").text(data1["name"]); 
   $("span#level").text(data1["level"]); 
   $("span#college").text(data2["college"]); 
   $("span#department").text(data2["department"]);
});

Hope this helps. Cheers

share|improve this answer
thank you very much – sasola May 7 '11 at 20:25
add comment (requires an account with 50 reputation)

Wrap them in JSON array:

[ {..}, {..}, {..}]

or, wrap them in another object:

{ "result1":{..}, "result2":{..} }
share|improve this answer
add comment (requires an account with 50 reputation)

You could return a JSON array with both objects as elements of the array. Have your servlet return JSON that has a structure like this one:

[{"name": "object1"}, {"name": "object2"}]

Then your javascript code can be something like this:

$.getJSON("MyServlet", paramenters, function (data){
        var data1 = data[0];
        var data2 = data[1];

        $("h3#name").text(data1["name"]);

        $("span#level").text(data1["level"]);

        $("span#college").text(data2["college"]);

        $("span#department").text(data2["department"]);

    });
share|improve this answer
thaaaaaaaaaak you ,, – sasola May 7 '11 at 20:25
add comment (requires an account with 50 reputation)

you're going to need to put both into a single json string like so

response.getWriter().write("[");
response.getWriter().write(json1);
response.getWriter().write(",");
response.getWriter().write(json2);
response.getWriter().write("]");

this puts them in a json array

you could also put them in a json object

response.getWriter().write("{\"object1\":");
response.getWriter().write(json1);
response.getWriter().write(",\"object2\":");
response.getWriter().write(json2);
response.getWriter().write("}");
share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.