字符窜转节点并打印节点树或转JSON
我司目前有一个这样子需求:数据库存储的是一条条记录,有一列是存储的目录结构的字符窜,如下:
xxxx公司/yy网区/zz电局/变电/bcdef/220kV石西站/主变区域/#2主变间隔/#2主变/#2主变
要把这些转化为JSON传给前端,下面是一些
1 package com.xx.yyyy; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 /** 7 * <p> Test </p> 8 * 9 * @author Aion.Liu 10 * @version v1.0.0 11 * @since 2020/6/23 2:26 下午 12 */ 13 public class Test { 14 15 public void parseString() { 16 17 NodTree nodeTree = new NodTree(); 18 nodeTree.setNodeValue("root"); 19 20 String str0 = "XX电网公司/XX网区/XX供电局/变电/aa巡维班/220kV111/主变区域/#2主变间隔/#2主变/#2主变"; 21 String str1 = "xx电网公司/XX网区/XX供电局/变电/bb巡维班/220kV222/主变区域/#1主变间隔/#1主变压器/#1主变"; 22 ... 43 String str23 = "XX电网公司/XX网区/XX供电局/变电/cc巡维班/220kV333/主变区域/#1主变间隔/#1主变/#1主变"; 44 String str24 = "XX电网公司/XX网区/XX供电局/变电/dd巡维班/220kV333/主变区域/#2主变间隔/#2主变/#2主变"; 45 46 String str25 = "YY电网公司/XX网区/XX供电局/变电/ee巡维班/220kV444/主变区域/#2主变间隔/#2主变/#2主变"; 47 48 List<String> stringList = new ArrayList<>(); 49 stringList.add(str0); 50 stringList.add(str1); 51 ... 73 stringList.add(str24); 74 stringList.add(str25); 75 76 for (String str : stringList) { 77 addNode(nodeTree, str.split("/")); 78 } 79 80 // print(nodeTree, 0); 81 82 nodeTree.toJson(nodeTree); 83 84 System.out.println(nodeTree.getBuffer()); 85 86 } 87 88 89 90 91 public void addNode(NodTree nodeTree, String[] strArr) { 92 NodTree creatNodeTree = null; 93 NodTree queryNode = null; 94 NodTree parentNode = nodeTree; 95 for (String str : strArr) { 96 queryNode = parentNode.hasSubNode(str); 97 if (null == queryNode) { 98 creatNodeTree = new NodTree(); 99 creatNodeTree.setNodeValue(str); 100 parentNode.addSunNode(creatNodeTree); 101 102 parentNode = creatNodeTree; 103 } else { 104 parentNode = queryNode; 105 } 106 } 107 } 108 109 110 111 class NodTree { 112 113 String nodeValue = null; 114 115 List<NodTree> nodeList = new ArrayList<NodTree>(); 116 117 StringBuffer buffer = new StringBuffer(); 118 119 public String getNodeValue() { 120 return nodeValue; 121 } 122 123 public void setNodeValue(String nodeValue) { 124 this.nodeValue = nodeValue; 125 } 126 127 public List<NodTree> getNodeList() { 128 return nodeList; 129 } 130 131 public void setNodeList(List<NodTree> nodeList) { 132 this.nodeList = nodeList; 133 } 134 135 public void addSunNode(NodTree nodeTree) { 136 nodeList.add(nodeTree); 137 } 138 139 public StringBuffer getBuffer() { 140 return buffer; 141 } 142 143 public void setBuffer(StringBuffer buffer) { 144 this.buffer = buffer; 145 } 146 147 public NodTree hasSubNode(String str) { 148 if (null != nodeList && !nodeList.isEmpty()) { 149 for (NodTree nodeTree : nodeList) { 150 if (nodeTree.nodeValue.equals(str)) { 151 return nodeTree; 152 } 153 } 154 } 155 return null; 156 } 157 158 /** 159 * 节点树打印 160 * @param nodeTree 节点树 161 * @param w 162 */ 163 public void print(NodTree nodeTree, int w) { 164 for (int i = 0; i < w; i++) { 165 System.out.print(" "); 166 } 167 System.out.println("|-" + nodeTree.nodeValue); 168 for (NodTree tree : nodeTree.getNodeList()) { 169 print(tree, w + 1); 170 } 171 } 172 173 174 /** 175 * 节点树 转 JSON 176 * @param nodeTree 节点树 177 */ 178 public void toJson(NodTree nodeTree) { 179 buffer.append("{\"data\":" + "\"" + nodeTree.getNodeValue() + "\""); 180 if (nodeTree.getNodeList().size() == 0) { 181 buffer.append(",\"children\":null}"); 182 return; 183 } else { 184 buffer.append(", \"children\":["); 185 for (NodTree n : nodeTree.getNodeList()) { 186 toJson(n); 187 buffer.append(","); 188 } 189 buffer = buffer.deleteCharAt(buffer.lastIndexOf(",")); 190 buffer.append("]}"); 191 } 192 } 193 } 194 195 196 public static void main(String[] args) { 197 Test t = new Test(); 198 t.parseString(); 199 } 200 }
执行结果转换查看:
理解数据格式后,才能更好的转换成为json提供给前端使用。
您的资助是我最大的动力!
金额随意,欢迎来赏!
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的
欢迎大家关注我的个人博客 Lycos | 小站 !由于最近没时间处理,原域名http://www.liuyuchuan.com暂时停止使用