zygod的gravatar头像
zygod 2016-07-11 16:18:48

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集合里面 大概结构如下 求大神帮忙

java如何将复杂字符串解析为树形结构?

所有回答列表(2)
serical的gravatar头像
serical  LV12 2016年7月12日

以下面字符串为例(三种情况都有),初略搞了下

&:(|(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,如图:

java如何将复杂字符串解析为树形结构?

评论(12) 最佳答案
zygod的gravatar头像
zygod  LV17 2016年7月11日

java如何将复杂字符串解析为树形结构?java如何将复杂字符串解析为树形结构?

顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友