当前位置:   article > 正文

前端传一个json集合字符串(其中包含对象),后端转换成arraylist_前端需要返回string但是后端返回值类型是arraylist可以吗

前端需要返回string但是后端返回值类型是arraylist可以吗

前台传一个封装好的里面装有对象的集合json字符串

  1. //节点结构体 (对象封装)
  2. function createObj(id, name, pId) {
  3. this.id = id;
  4. this.name = name;
  5. this.pId = pId;
  6. }
  7. createObj.prototype.sayId = function() {
  8. alert(this.id);
  9. }
  10. createObj.prototype.sayName = function() {
  11. alert(this.name);
  12. }
  13. createObj.prototype.saypId = function() {
  14. alert(this.pId);
  15. }
  16. //向后台提交选中的树节点信息
  17. function fff() {
  18. var treeObj = $.fn.zTree.getZTreeObj("treeSelect");
  19. nodes = treeObj.getCheckedNodes(true);
  20. var ss = new Array();//创建list集合
  21. //var list= [];
  22. v = "";
  23. for (var i = 0; i < nodes.length; i++) {
  24. var person = new createObj(nodes[i].id, nodes[i].name, nodes[i].pId);
  25. ss[i] = person;
  26. //v+="name:"+ nodes[i].name + ",";
  27. //alert(nodes[i].id); //获取选中节点的值
  28. //v+="id:"+ nodes[i].id + ",";
  29. //v+="PId:"+ nodes[i].pId + ",";
  30. //console.log(nodes);
  31. }
  32. //console.log(ss);
  33. var appop = JSON.stringify(ss);
  34. console.log(appop);
  35. var jsonStrings = encodeURIComponent(appop);
  36. console.log(jsonStrings);
  37. $.ajax({
  38. url : "../User/insertPermissions?jsonStrings="
  39. + jsonStrings,
  40. type : "post",
  41. dataType : "json",
  42. async : false,
  43. contentType : 'application/x-www-form-urlencoded;charset=UTF-8',
  44. success : function(data) {
  45. alert("提交成功");
  46. console.log(data);
  47. },
  48. error : function(msg) {
  49. alert("ajax连接异常:" + msg);
  50. }
  51. });

 

 

 后台接收

  1. /*
  2. * 展示提交的树节点信息
  3. */
  4. @RequestMapping(value = "/insertPermissions", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
  5. @ResponseBody
  6. public Map<String, String> insertPermissions(@RequestParam(value = "jsonStrings") String jsonStrings,
  7. HttpServletRequest request) throws IOException {
  8. URLDecoder.decode(jsonStrings, "UTF-8");
  9. List<Trees> trees = new ArrayList<Trees>();
  10. JSONArray jsonArray = JSONArray.fromObject(jsonStrings);
  11. for (int i = 0; i < jsonArray.size(); i++) {
  12. JSONObject jsonObject = jsonArray.getJSONObject(i);
  13. Trees trees2 = (Trees) JSONObject.toBean(jsonObject, Trees.class);
  14. trees.add(trees2);
  15. }
  16. System.out.println(trees);
  17. for (Trees tr : trees) {
  18. System.out.println(tr.getName());
  19. }
  20. Map<String, String> map = new HashMap<String, String>();
  21. map.put("result", "true");
  22. return map;
  23. }

 

效果:这个集合可以通过增强for循环遍历

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/509684
推荐阅读
相关标签
  

闽ICP备14008679号