前台传一个封装好的里面装有对象的集合json字符串
//节点结构体 (对象封装)
function createObj(id, name, pId) {
this.id = id;
this.name = name;
this.pId = pId;
}
createObj.prototype.sayId = function() {
alert(this.id);
}
createObj.prototype.sayName = function() {
alert(this.name);
}
createObj.prototype.saypId = function() {
alert(this.pId);
}
//向后台提交选中的树节点信息
function fff() {
var treeObj = $.fn.zTree.getZTreeObj("treeSelect");
nodes = treeObj.getCheckedNodes(true);
var ss = new Array();//创建list集合
//var list= [];
v = "";
for (var i = 0; i < nodes.length; i++) {
var person = new createObj(nodes[i].id, nodes[i].name, nodes[i].pId);
ss[i] = person;
//v+="name:"+ nodes[i].name + ",";
//alert(nodes[i].id); //获取选中节点的值
//v+="id:"+ nodes[i].id + ",";
//v+="PId:"+ nodes[i].pId + ",";
//console.log(nodes);
}
//console.log(ss);
var appop = JSON.stringify(ss);
console.log(appop);
var jsonStrings = encodeURIComponent(appop);
console.log(jsonStrings);
$.ajax({
url : "../User/insertPermissions?jsonStrings="
+ jsonStrings,
type : "post",
dataType : "json",
async : false,
contentType : 'application/x-www-form-urlencoded;charset=UTF-8',
success : function(data) {
alert("提交成功");
console.log(data);
},
error : function(msg) {
alert("ajax连接异常:" + msg);
}
});
后台接收
/*
* 展示提交的树节点信息
*/
@RequestMapping(value = "/insertPermissions", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public Map<String, String> insertPermissions(@RequestParam(value = "jsonStrings") String jsonStrings,
HttpServletRequest request) throws IOException {
URLDecoder.decode(jsonStrings, "UTF-8");
List<Trees> trees = new ArrayList<Trees>();
JSONArray jsonArray = JSONArray.fromObject(jsonStrings);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Trees trees2 = (Trees) JSONObject.toBean(jsonObject, Trees.class);
trees.add(trees2);
}
System.out.println(trees);
for (Trees tr : trees) {
System.out.println(tr.getName());
}
Map<String, String> map = new HashMap<String, String>();
map.put("result", "true");
return map;
}
效果:这个集合可以通过增强for循环遍历