ztree 后台返回json数据的拼接

挖掘机不好开 发布于 2013/12/11 12:13
阅读 8K+
收藏 1

@红薯 你好,想跟你请教个问题:

ztree 后台从webservece里面获取数据获取的数据类型是List<org>,org字段如下

  private String id;
  private String name;
  private String desc;
  List<Org> childOrg;

现在想递归List<org> 这个数据成Json格式,返回前台显示成树,求思路。

 List<Org> list  = null;
         DTAuthorityDataSourceFactory factory = new DTAuthorityDataSourceFactory();
         try {
             list =  factory.getAuthorityDataSource("http://172.16.60.143/manager/rpc/ws/orgserver/getAllOrgInfo?wsdl").getAllOrgs();
         } catch (MalformedURLException e) {
              e.printStackTrace();
         }
         if (list.size()==0)
            {
              return null;
            }
        String orgs = list.toString();
        Document doc = StringUtils.parthStringXml(orgs);
        NodeList orgNodes = doc.getChildNodes();
        resolveOrgXml(orgNodes, orgs);
        System.out.println(list.size());
        PrintWriter out = response.getWriter();
        String str = JSON.toJSONString(list);
        System.out.println(JSON.toJSONString(str));
        out.print(new String(str.getBytes("utf-8"),"8859_1"));
        out.close();
        return null;
     }
     private void resolveOrgXml(NodeList orgNodes, Org parentOrg)
      {
        if (orgNodes.getLength() == 0)
        {
          return;
        }
        for (int i = 0; i < orgNodes.getLength(); i++)
        {
          Node tempNode = orgNodes.item(i);
          NamedNodeMap tempNnm = tempNode.getAttributes();
          String tempId = tempNnm.getNamedItem("id").getNodeValue();
          String tempName = tempNnm.getNamedItem("name").getNodeValue();
          Node tempDescNode = tempNnm.getNamedItem("desc");
          Org org = new Org();
          org.setId(tempId);
          org.setName(tempName);
          if (tempDescNode != null)
          {
            org.setDesc(tempDescNode.getNodeValue());
          }
          if (parentOrg == null)
          {
            this.orgs.add(org);
          } else {
            List childOrg = parentOrg.getChildOrg();
            if (childOrg == null)
            {
              childOrg = new ArrayList();
              childOrg.add(org);
              parentOrg.setChildOrg(childOrg);
            } else {
              parentOrg.getChildOrg().add(org);
            }
          }
          NodeList childNodeList = tempNode.getChildNodes();
          if ((childNodeList == null) || (childNodeList.getLength() <= 0))
            continue;
          resolveOrgXml(childNodeList, org);
        }

加载中
0
陈阳阳阳
陈阳阳阳
其实可以使用simple数据格式啊,然后使用其它类库直接将对象序列化为json对象就可以了,没必要自己写递归来构造..
0
巴顿
巴顿

你看下zTree上面的一个例子

它是这样的

 var zNodes =[

44             { id:1, pId:0, name:"父节点1 - 展开", open:true},
45             { id:11, pId:1, name:"父节点11 - 折叠"},
46             { id:111, pId:11, name:"叶子节点111"},
47             { id:112, pId:11, name:"叶子节点112"},
48             { id:113, pId:11, name:"叶子节点113"},
49             { id:114, pId:11, name:"叶子节点114"},
50             { id:12, pId:1, name:"父节点12 - 折叠"},
51             { id:121, pId:12, name:"叶子节点121"},
52             { id:122, pId:12, name:"叶子节点122"},
53             { id:123, pId:12, name:"叶子节点123"},
54             { id:124, pId:12, name:"叶子节点124"},
55             { id:13, pId:1, name:"父节点13 - 没有子节点", isParent:true},
56             { id:2, pId:0, name:"父节点2 - 折叠"},
57             { id:21, pId:2, name:"父节点21 - 展开", open:true},
58             { id:211, pId:21, name:"叶子节点211"},
59             { id:212, pId:21, name:"叶子节点212"},
60             { id:213, pId:21, name:"叶子节点213"},
61             { id:214, pId:21, name:"叶子节点214"},
62             { id:22, pId:2, name:"父节点22 - 折叠"},
63             { id:221, pId:22, name:"叶子节点221"},
64             { id:222, pId:22, name:"叶子节点222"},
65             { id:223, pId:22, name:"叶子节点223"},
66             { id:224, pId:22, name:"叶子节点224"},
67             { id:23, pId:2, name:"父节点23 - 折叠"},
68             { id:231, pId:23, name:"叶子节点231"},
69             { id:232, pId:23, name:"叶子节点232"},
70             { id:233, pId:23, name:"叶子节点233"},
71             { id:234, pId:23, name:"叶子节点234"},
72             { id:3, pId:0, name:"父节点3 - 没有子节点", isParent:true}


所以比如你要返回的对象是person,当然这个person在数据库中应该是

id,pid这样的

你查出所有的person,得到一个personList,

然后加入依赖包fastJson,调用JSONObject(personList)得到一个String类型的数据比如str,然后拿到PrintWriter out,通过out.write(str)就能将符合上面那种类型的数据返回到前台了。

返回顶部
顶部