4 回答

TA貢獻(xiàn)1883條經(jīng)驗 獲得超3個贊
可以使用Gson庫,先寫一個類用于存儲每個學(xué)生的信息,屬性名稱和類型都要一一對應(yīng),假設(shè)這個類的名稱為Student,然后通過Student[] students = new Gson().fromJson(jsonString, Student[].class);獲得一個存有所有學(xué)生實例的數(shù)組。不知道能不能這樣操作,自己摸索下吧

TA貢獻(xiàn)1801條經(jīng)驗 獲得超8個贊
由于你后臺return "wanglongtest.html",所以前臺ajax接收是一個
html對象,則前臺可以按如下代碼處理:
success: function(rs){
$("#testDiv").html(rs);
}
html:
<!-- ajax 要填充的內(nèi)容 -->
<div id="testDiv">
</div>
------------------------------------
如果你想接收后臺的類型為json,那么后臺servlet方法中只需要return null;
完整代碼見如下:
response.setHeader("pragma", "no-cache");
response.setHeader("cache-control", "no-cache");
//設(shè)置響應(yīng)格式和字符集(與前端頁面一致,否則會有亂碼問題)
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
Map model = new HashMap();
model.put("zhangsan",true);
JSONObject json = new JSONObject(model); //將一個map對象實例化成一個json對象
out.write(json.toString());
out.flush();
out.close();
return null;
前臺js:
$.ajax({
type: "POST",
url: "請求地址",
dataType: "json", //指定請求的數(shù)據(jù)類型
data:"type=1", //傳到后臺的參數(shù),后臺可以通過request.getParameter("type")獲取
success:function(rs){
alert(rs.zhangsan); //我這里以彈出框的方式顯示zhangsan對應(yīng)的value
//也可以為html中的標(biāo)簽賦值
$("#testAjax").val(rs.zhangsan);
}
},"json")
});
html:
<input id="testAjax" type="text" value="" />

TA貢獻(xiàn)1808條經(jīng)驗 獲得超4個贊
$.ajax({
url : "${rootUrl}traffic/check.service",
type : "GET",
data : "type=1",
dataType : "html",
success:function(msg){
$("#resultXml").html(msg);
},
error:function(msg){
$("#resultXml").html("請求失敗");
}
});
添加回答
舉報