java如何将复杂字符串解析为树形结构?
&:506,529,530,531,532,533,534,535,536,508,(|(2):1,2,3,3707,3706,12656)
&:(|(2):4,5,3708,3709),609,610,6042,612,7980,(|:845,839),535,7674,7979,11531
&:9,4729,18,19,20,41,43,55,65,144,145,(|:(&+商业企业管理:58,182,185,7547),(&:58,146,147,148),(&+国际企业管理:12,98,148,149)),(|(2):1,2,3,3707,3706,12656)
&:(|:4,3708),(|:15,16,17,(&:1457,765,3122)),(|:21,(&:4183,4184)),51,52,77,157,158,160,207,208,6270,7566,9817
&:(|:4,3708),(|:15,16,17,(&:1457,765,3122)),(|:21,(&:4183,4184)),51,52,77,157,158,160,207,208,6270,7566,9817
&:(|:4,3708),(|:15,16,17,(&:1457,765,3122)),(|:21,(&:4183,4184)),51,52,77,157,158,160,207,208,6270,7566,9817
&:41,888,889,890,891,892,893,894,895,896,897,898,899,900,901,902,903,(|:904,905),(|(2):1,2,3,3707,3706,12656)
&:(|:4,3708),(|:994),(|:15,7648,(&:1457,765,3122)),(|:995),910,996,911,912,906,907,997,998,908,909,913,914,915,916,18516
&:(|:4,3708),(|:994),(|:15,7648,(&:1457,765,3122)),(|:995),910,996,911,912,906,907,997,998,908,909,913,914,915,916,18516
&:(|:4,3708),(|:994),(|:15,7648,(&:1457,765,3122)),(|:995),910,996,911,912,906,907,997,998,908,909,913,914,915,916,18516
&:(|:4,3708),(|:994),(|:15,7648,(&:1457,765,3122)),995,889,910,996,911,912,906,907,997,998,908,909,913,914,915,916,18516
&:(|:4,3708),(|:(|:15,16,17),(&:34,277),(|(3):34,277,1457,765,3122)),(|:6088,7700),51,52,800,(|:54,19170),(|:41,1726),(|:182,5969),6089,6090,6091,6092,6093,(|:10272,18948)
&:(|:4,3708),(|:(|:15,16,17),(&:34,277),(|(3):34,277,1457,765,3122)),(|:6088,7700),51,52,800,(|:54,19170),(|:41,1726),(|:182,5969),6089,6090,6091,6092,6093,(|:10272,18948)
&:12,18,19,5363,5365,5366,5367,5368,5369,5370,5371,(|(3):5361,5362,5364,5372,20,41,89,144,148,182),(|(2):12656,3706,3707)
&:(|(2):4,5,3708,3709),9,(|:15,16,17),5373,5375,5376,5377,5378,5379,5380,(|(2):43,5374,1574,55,98,101,147,151,152,2628,4183,4184,7009),18595
&:(|:4,3708),7481,7010,7008,7007,7006,7009,9819,(|:21,(&:4183,4184),(&:2375,6996)),(|:15,16,17,(&:7693,7694,7695),(&:7011,7012))
&:341,3312,3313,(|:3314,12324),3315,3316,3317,(|:3318,12325),3319,3320,3321,(|(2):1,2,3,3707,3706,12656)
&:(|(2):4,5,3708,3709),609,610,6042,612,7980,(|:845,839),535,7674,7979,11531
&:(|:11,4729),(|:522,7704),593,594,595,596,597,794,795,(|(2):1,2,3,3707,3706,12656)
&:(|:4,3708),9,7382,7383,7373,7374,7375,7376,7377,602,76,92,7378,7379,7380,7381,11522
有如上的数据
&:(|:4,3708),9,7382,7383,7373,7374,7375,7376,7377,602,76,92,7378,7379,7380,7381,11522
这里面&:指必修
|:指多选一
|(N):这种指选修N
上面黑体字“(|:4,3708)”指多选一里面有4和3708
求大神帮分析 把他们放到一个有层级关系的list集合里面 大概结构如下 求大神帮忙
以下面字符串为例(三种情况都有),初略搞了下
&:(|(2):4,5,3708,3709),609,610,6042,612,7980,(|:845,839),535,7674,7979,11531
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import net.sf.json.JSONArray; public class Test { public static void main(String[] args) { String s = "&:(|(2):4,5,3708,3709),609,610,6042,612,7980,(|:845,839),535,7674,7979,11531"; String regex = "\\(" + "[^\\(\\)]*" + "(" + "\\(" + "[^\\(\\)]*" + "\\)" + "[^\\(\\)]*" + ")*" + "\\)"; String required = s.replaceAll(regex, "").replaceAll("&:,", "").replaceAll(",,", ","); System.out.println(required); // 必修节点 Map<String, Object> requiredNode = new HashMap<>(); List<Map<String, Object>> requiredList = new ArrayList<>(); if(null != required && required.length() > 0) { for(String str : required.split(",")) { Map<String, Object> node = new HashMap<>(); node.put("text", str); requiredList.add(node); } } requiredNode.put("text", "必修"); // 多选一节点 Map<String, Object> optionsNode = new HashMap<>(); List<Map<String, Object>> optionsList = new ArrayList<>(); optionsNode.put("text", "(多选一)"); // 选修N Map<String, Object> optionsNNode = new HashMap<>(); List<Map<String, Object>> optionsNList = new ArrayList<>(); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(s); while(matcher.find()) { String result = matcher.group(); System.out.println(result); String flag = String.valueOf(result.charAt(2)); String N = result.substring(result.lastIndexOf("(")+1, result.indexOf(")")); String str = result.substring(result.indexOf(":")+1, result.length()-1); System.out.println(str); if (flag.equals(":")) { if(null != str && str.length() > 0){ for(String text : str.split(",")) { Map<String, Object> node = new HashMap<>(); node.put("text", text); optionsList.add(node); } } } else if (flag.equals("(")) { optionsNNode.put("text", "(选修"+N+")"); if(null != str && str.length() > 0){ for(String text : str.split(",")) { Map<String, Object> node = new HashMap<>(); node.put("text", text); optionsNList.add(node); } } } } // 组装数据结构 optionsNode.put("children", optionsList); optionsNNode.put("children", optionsNList); requiredList.add(optionsNode); requiredList.add(optionsNNode); requiredNode.put("children", requiredList); List<Map<String, Object>> root = new ArrayList<>(); root.add(requiredNode); System.out.println(JSONArray.fromObject(root)); } }
生成JSON字符串:
[{"text":"必修","children":[{"text":"609"},{"text":"610"},{"text":"6042"},{"text":"612"},{"text":"7980"},{"text":"535"},{"text":"7674"},{"text":"7979"},{"text":"11531"},{"text":"(多选一)","children":[{"text":"845"},{"text":"839"}]},{"text":"(选修2)","children":[{"text":"4"},{"text":"5"},{"text":"3708"},{"text":"3709"}]}]}]
基本符合Easyui Tree组件,但是没有ID需要你自己根据业务来决定ID,如图:
- 等 最代码怎么获取牛币啊?
- 完 谁来告诉我最代码上线的时间,答对者给5牛币,先来先得
- 等 牛友们,大家好,你们做程序员多久了?现在还好吗?
- 完 在微信打开的页面里进行app下载
- 等 最代码2014年欢乐聚声会
- 完 mysql如何查询表数据并且对3个字段降序的SQL?
- 完 最代码牛币机制改革
- 完 成功的在bae上使用了自定义运行环境 jetty+nginx的组合,大家对jetty+nginx优化有哪些心得?
- 完 进来分享一下各位牛牛是如何加入最代码大家庭的?
- 等 为什么java BufferedImage类处理大图直接抛出内存溢出的异常?
- 等 最代码是否开发手机app客户端?
- 完 java程序员学习哪些java的技术?java有哪些框架?都能做哪方面的开发?
- 等 php格式网页文件怎么运行?
- 等 Java volatile值获取的问题
- 等 前端vue,拦截了登录后台后,返回的token,requests拦截token,但是发送请求的时候,就出现跨越异常
- 等 大专本科计算机科班怎么找到Java工作?
- 等 eclipse怎么把三个java swing游戏项目合成一个项目?
- 完 伙伴们,大家都有什么好的解压方式么,分享一下~
- 完 三四线城市,6、7k,运维工作,索然无味,想去辞职上培训,各位牛牛有什么建议嘛
- 等 jsp页面输入中文变成问号
- 等 JPA在线上运行一段时间后报错Caused by: java.lang.IncompatibleClassChangeError: null
- 等 PHP 这个规则用preg_match_all怎么写
- 等 大佬们,有没有知道Alfresco如何配置LDAP登录呢?
- 等 php的install目录是框架带的吗?