您好,登錄后才能下訂單哦!
樹形JSON列表怎么利用java與mysql進行拼接?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
例如:
[ { "name": "商品目錄", "pid": "-1", "id": "1", "children": [ { "name": "日用品", "pid": "1", "id": "11", "children": [ { "name": "洗發水", "pid": "11", "id": "111", "children": [ { "name": "霸王", "pid": "111", "id": "1111", "children": [] } ] } ] }, { "name": "食品", "pid": "1", "id": "12", "children": [] } ] } ]
整體思路分為兩步,第一步獲取目錄及其所有子目錄,獲取后的列表形式如下:
[ {"id":"1","pid":"-1","name":"商品目錄"}, {"id":"11","pid":"1","name":"日用品"}, {"id":"12","pid":"1","name":"食品"}, {"id":"111","pid":"11","name":"洗發水"}, {"id":"1111","pid":"111","name":"霸王"} ]
第二步,利用遞歸思想拼裝該數據,拼裝方法的工具類如下:
package *.*.*; import net.sf.json.JSONArray; import java.util.ArrayList; import java.util.List; /** * 構造目錄JSON樹 * Created by fukang on 2017/5/26 0026. */ public class TreeBuilder { List<Node> nodes = new ArrayList<>(); public String buildTree(List<Node> nodes) { TreeBuilder treeBuilder = new TreeBuilder(nodes); return treeBuilder.buildJSONTree(); } public TreeBuilder() { } public TreeBuilder(List<Node> nodes) { super(); this.nodes = nodes; } // 構建JSON樹形結構 public String buildJSONTree() { List<Node> nodeTree = buildTree(); JSONArray jsonArray = JSONArray.fromObject(nodeTree); return jsonArray.toString(); } // 構建樹形結構 public List<Node> buildTree() { List<Node> treeNodes = new ArrayList<>(); List<Node> rootNodes = getRootNodes(); for (Node rootNode : rootNodes) { buildChildNodes(rootNode); treeNodes.add(rootNode); } return treeNodes; } // 遞歸子節點 public void buildChildNodes(Node node) { List<Node> children = getChildNodes(node); if (!children.isEmpty()) { for (Node child : children) { buildChildNodes(child); } node.setChildren(children); } } // 獲取父節點下所有的子節點 public List<Node> getChildNodes(Node pnode) { List<Node> childNodes = new ArrayList<>(); for (Node n : nodes) { if (pnode.getId().equals(n.getPid())) { childNodes.add(n); } } return childNodes; } // 判斷是否為根節點 public boolean rootNode(Node node) { boolean isRootNode = true; for (Node n : nodes) { if (node.getPid().equals(n.getId())) { isRootNode = false; break; } } return isRootNode; } // 獲取集合中所有的根節點 public List<Node> getRootNodes() { List<Node> rootNodes = new ArrayList<>(); for (Node n : nodes) { if (rootNode(n)) { rootNodes.add(n); } } return rootNodes; } public static class Node { private String id; private String pid; private String name; private List<Node> children; public Node() { } public Node(String id, String pid, String name) { super(); this.id = id; this.pid = pid; this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPid() { return pid; } public void setPid(String pid) { this.pid = pid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Node> getChildren() { return children; } public void setChildren(List<Node> children) { this.children = children; } } }
在Controller中的調用方法是:
@RequestMapping("/bulidJsonTree") @ResponseBody public String buildJsonTree(HttpServletRequest request) { // 獲取全部目錄節點 List<Node> nodes = iGoodsDirSvc.getAllDirList(); // 拼裝樹形json字符串 String json = new TreeBuilder().buildTree(nodes); return json; }
其中iGoodsDirSvc.getAllDirList()
方法需要將取到的數據轉為Node類型:
String hql = "select id as id,pId as pid,name as name from Directory"; Query query = factory.getCurrentSession().createQuery(hql) .setResultTransformer(Transformers.aliasToBean(TreeBuilder.Node.class)); return query.list();
看完上述內容,你們掌握樹形JSON列表怎么利用java與mysql進行拼接的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。