servlet部分代码,依赖net.sf.json包
1.返回json对象
2.返回json数组
前台jquery部分jsonarray
前台jquery部分jsonobject
1.返回json对象
//初始化查询条件流域
HashMap<String,ArrayList<HashMap>> resultMap = new HashMap<String,ArrayList<HashMap>>();
ArrayList<HashMap> lyList = null;//流域
try {
String lySQL = "select distinct(syslycode),syslyname from t_inventoryList where syslycode is not null";
lyList = ab.query(lySQL);
resultMap.put("ly", lyList);
JSONObject jsonObj = JSONObject.fromObject(resultMap);
toRespdb(response,jsonObj);
} catch (Exception e) {
e.printStackTrace();
}
2.返回json数组
try {
QJMNUtil util=new QJMNUtil();
List<Map<String,Object>> seriesList = util.getFushionContentForHYPFFX(request,ab);
JSONArray jsonArray = JSONArray.fromObject(seriesList);
toRespdb(response,jsonArray);
}
catch (Exception e) {
e.printStackTrace();
}
/**
* 向前台写数据
* @param resp
* @param content
*/
public void toRespdb(HttpServletResponse resp,Object content){
try {
PrintWriter out = resp.getWriter();
out.print(content);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
前台jquery部分jsonarray
//改变流域时,修改河流下拉框数据
function modifyhl(obj){
var lyval=$(obj).val();
//改变流域时将河流文本框置空
$("#hllist").val("");
$("input:hidden[name='wsystemcode3']").val("");
$.ajax({
url: '../qdfx.do',
cache: false, //不从缓存中取数据
data: {todo:'QueryByHl',lycode:lyval,type:'eidt'},
type:'post',
async:false,
dataType:'json',
timeout:100000,//超时
error:function (msg) {
alert(msg.responseText);
},
success:function(dbresp)
{
$("#loadId").remove();
$.fn.zTree.init($("#treeTrade"), setting,dbresp);
}
});
}
前台jquery部分jsonobject
//加载流域
function loadly(){
$.ajax({
url: '../qdfx.do',
cache: false, //不从缓存中取数据
data: {todo:'QueryByLy'},
type:'post',
async:false,
dataType:'json',
timeout:100000,//超时
error:function (msg) {
alert(msg.responseText);
},
success:function(dbresp)
{
var lyList = dbresp.ly;
var trade = dbresp.trade;
for(i=0;i<lyList.length;i++){
$("#lylist").append("<option value='"+lyList[i].SYSLYCODE+"'>"+lyList[i].SYSLYNAME+"</option>");
}
}
});
}