3 回答

TA贡献1860条经验 获得超8个赞
去掉开头和结尾的[和]。21:[1,2,3],22:[1,2,3]
2.对]进行字符串分割,
对于每个元素,字符串再次拆分为:[。
第一个元素是您的 Map 键 第二个元素(逗号分隔)可以使用数组转换转换为列表

TA贡献1966条经验 获得超4个赞
一些直接的常规:
String s = "[21:[1,2,3],22:[1,2,3],23:[6:[1,2],7:[3,4]]]"
Map res = Eval.me s
assert [1,2,3] == res[22]
assert [3,4] == res[23][7]

TA贡献1808条经验 获得超4个赞
删除方括号( [ )
removeAll("\\[","")
删除最后一个方括号 ( ] ,") 以及结尾的逗号 ( , ) 并替换为全冒号 ( : )
removeAll("\\[",":")
替换字符串末尾的双方括号
removeAll("\\]]","")
使用分割剩余的字符串
split(":")
然后循环
String x = "[21:[1,2,3],22:[1,2,3]]";
final HashMap<String,List<String>> test = new HashMap<>();
final String s = x.replaceAll("\\[", "").replaceAll("\\],", ":").
replaceAll("\\]]", "");
final String[] split = s.split(":");
for(int i =0;i<split.length;i++)
test.put(split[i],Arrays.asList(split[++i]));
test.forEach((k,v)-> System.out.println(k +" " +v))
结果
添加回答
举报