这个在开发中经常用到
response.setContentType("application/json;charset=utf-8");
PrintWriter out = response.getWriter();
List<Department> department = departmentService.listDeptByGradeId(id);
JSONArray jsonStr = JSONArray.fromObject(department);
out.println(jsonStr.toString());
out.close();
//根据年级编号查询院系ajax
function findByGradeId(id,path){
var dept = document.getElementById("dept");
dept.length=1;
if(id==""){
alert("请选择年级~!");
}else{
$.get(path+"/admin/department/ajaxgradeid",{"id":id},function(data){
var redata = eval(data);
for(var i=0;i<redata.length;i++){
$(dept).append("<option value='"+redata[i].departmentid+"'>"+redata[i].bumen.bumenname+"</option>");
}
});
}
}
form 表单与后台jsonobject
// 保存主表表单
function saveForm() {
var bean = $('#AddForm').serializeArray();
var r = $('#AddForm').form('validate');
if (r) {
var beanJson = convertToJson(bean);
var beanJsonString = JSON.stringify(beanJson);
$.post(baseurl + 'platform/applicationLiaAction/save.json', {
jsonString : beanJsonString
}, function(status) {
if (status.flag == "success") {
$.messager.show({
title : '提示',
msg : "保存成功!"
});
loadApplicationAdd();
} else {
$.messager.show({
title : '提示',
msg : "保存失败!"
});
}
dataGrid.load();
});
}
};
//前端的contojson
function SFconvertToJson(formValues) {
var jsonObject = {};
for ( var i = 0; i < formValues.length; i++) {
var formValue = formValues[i];
var name = formValue.name;
var value = formValue.value;
if (jsonObject[name]) {
if (name.indexOf('.') < 0) {
jsonObject[name] = jsonObject[name] + "," + value;
continue;
}
} else {
if (name.indexOf('.') < 0) {
jsonObject[name] = value;
continue;
} else {
var simpleNames = name.split('.');
var obj = jsonObject;
for ( var j = 0; j < simpleNames.length - 1; j++) {
var simpleName = simpleNames[j];
if (simpleName.indexOf('[') < 0) {
if (obj[simpleName] == null) {
obj[simpleName] = {};
}
obj = obj[simpleName];
} else {
var arrNames = simpleName.split('[');
var arrName = arrNames[0];
var arrIndex = parseInt(arrNames[1]);
if (obj[arrName] == null) {
obj[arrName] = [];
}
obj = obj[arrName];
multiChooseArray = jsonObject[arrName];
if (obj[arrIndex] == null) {
obj[arrIndex] = {};
}
obj = obj[arrIndex];
}
}
if (obj[simpleNames[simpleNames.length - 1]]) {
var temp = obj[simpleNames[simpleNames.length - 1]];
obj[simpleNames[simpleNames.length - 1]] = temp;
} else {
obj[simpleNames[simpleNames.length - 1]] = value;
}
}
}
}
return jsonObject;
}
@RequestMapping(value = "/save")
public ModelAndView save(HttpServletResponse response, HttpServletRequest request) throws Exception {
String jsonString = request.getParameter("jsonString");
JSONObject jsonObject = JSONObject.fromObject(jsonString);
JSONUtils.getMorpherRegistry().registerMorpher(
new DateMorpher(new String[] { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "yyyy-MM-dd't'HH:mm:ss" }, new Date()));
ApplicationInfo bean = (ApplicationInfo) JSONObject.toBean(jsonObject, ApplicationInfo.class);
ApplicationInfo hasBean = this.applicationInfoService.getBeanById(bean.getId());
if (hasBean != null) {
ModelAndView mav = new ModelAndView();
mav.addObject("flag", OpResult.success);
mav.setView(null);
return mav;
}
bean.setStatus("10");
this.applicationInfoService.insertBean(bean);
ModelAndView mav = new ModelAndView();
mav.addObject("flag", OpResult.success);
mav.setView(null);
return mav;
}