我正在使用jsp和servlet构建Web应用程序,我从jsp发送ajax请求,我想从servlet返回两个json对象。我尝试执行以下操作但代码不起作用。
//在jquery中我写了这段代码
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"]);
});
//在servlet中我编写了这段代码
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);
有人可以帮助我吗?
答案 0 :(得分:20)
你应该这样做:
服务器端:
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);
客户方:
$.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"]);
});
希望这会有所帮助。干杯
答案 1 :(得分:2)
将它们包装在JSON数组中:
[ {..}, {..}, {..}]
或者将它们包装在另一个对象中:
{ "result1":{..}, "result2":{..} }
答案 2 :(得分:1)
您可以返回一个JSON数组,其中两个对象都是数组的元素。让您的servlet返回具有如下结构的JSON:
[{"name": "object1"}, {"name": "object2"}]
然后你的javascript代码可能是这样的:
$.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"]);
});
答案 3 :(得分:1)
你需要将两者放入一个像这样的单个json字符串
response.getWriter().write("[");
response.getWriter().write(json1);
response.getWriter().write(",");
response.getWriter().write(json2);
response.getWriter().write("]");
这将它们放在json数组中
您也可以将它们放在json对象中
response.getWriter().write("{\"object1\":");
response.getWriter().write(json1);
response.getWriter().write(",\"object2\":");
response.getWriter().write(json2);
response.getWriter().write("}");
答案 4 :(得分:0)
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
...
resp.setContentType("application/json");
resp.setCharacterEncoding("utf-8");
ArrayList<Object> obj_arr = new ArrayList<Object>();
obj_arr.add(obj1);
obj_arr.add(obj2);
Gson gson = new Gson();
String tmp = gson.toJson(obj_arr);
resp.getWriter().write(tmp);
}
在前端,对于我们获得的数据,我们可以使用data[0]
来检索obj1
和data[1]
来检索obj2
。代码将是这样的(我在这里使用ajax):
$('#facts_form').submit(function (e) {
e.preventDefault();
var data = new FormData(this);
var url = 'import';
$.ajax({
url: url,
type: "POST",
data: data,
processData: false,
contentType: false,
async: false,
cache: false,
success: function (data) {
for (var i = 1; i < data.length; i++){
//do whatever
}
},
error: function (xhr, status, error) {
alert(status + "\n" + error);
}
});
});