您好,登錄后才能下訂單哦!
樹(shù)形JSON列表怎么利用java與mysql進(jìn)行拼接?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
例如:
[ { "name": "商品目錄", "pid": "-1", "id": "1", "children": [ { "name": "日用品", "pid": "1", "id": "11", "children": [ { "name": "洗發(fā)水", "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":"洗發(fā)水"}, {"id":"1111","pid":"111","name":"霸王"} ]
第二步,利用遞歸思想拼裝該數(shù)據(jù),拼裝方法的工具類如下:
package *.*.*; import net.sf.json.JSONArray; import java.util.ArrayList; import java.util.List; /** * 構(gòu)造目錄JSON樹(shù) * 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; } // 構(gòu)建JSON樹(shù)形結(jié)構(gòu) public String buildJSONTree() { List<Node> nodeTree = buildTree(); JSONArray jsonArray = JSONArray.fromObject(nodeTree); return jsonArray.toString(); } // 構(gòu)建樹(shù)形結(jié)構(gòu) public List<Node> buildTree() { List<Node> treeNodes = new ArrayList<>(); List<Node> rootNodes = getRootNodes(); for (Node rootNode : rootNodes) { buildChildNodes(rootNode); treeNodes.add(rootNode); } return treeNodes; } // 遞歸子節(jié)點(diǎn) public void buildChildNodes(Node node) { List<Node> children = getChildNodes(node); if (!children.isEmpty()) { for (Node child : children) { buildChildNodes(child); } node.setChildren(children); } } // 獲取父節(jié)點(diǎn)下所有的子節(jié)點(diǎn) 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; } // 判斷是否為根節(jié)點(diǎn) public boolean rootNode(Node node) { boolean isRootNode = true; for (Node n : nodes) { if (node.getPid().equals(n.getId())) { isRootNode = false; break; } } return isRootNode; } // 獲取集合中所有的根節(jié)點(diǎn) 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中的調(diào)用方法是:
@RequestMapping("/bulidJsonTree") @ResponseBody public String buildJsonTree(HttpServletRequest request) { // 獲取全部目錄節(jié)點(diǎn) List<Node> nodes = iGoodsDirSvc.getAllDirList(); // 拼裝樹(shù)形json字符串 String json = new TreeBuilder().buildTree(nodes); return json; }
其中iGoodsDirSvc.getAllDirList()
方法需要將取到的數(shù)據(jù)轉(zhuǎn)為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();
看完上述內(nèi)容,你們掌握樹(shù)形JSON列表怎么利用java與mysql進(jìn)行拼接的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。