前端传一个json集合字符串(其中包含对象),后端转换成arraylist

前台传一个封装好的里面装有对象的集合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循环遍历

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值