Java中[xxx:xxx,aaa:aaa]格式字符串解析
String str = "[name:张三,age:18,phone:15888887777,email:15888887777@qq.com]";
//转换成json数组 package net.sf.json
JSONArray input = JSONArray.fromObject(str);
input.stream().map(e -> {
//将json数组转换成map并返回
Map<String,String> map = new HashMap<>(2);
String[] kv = e.toString().split(":");
map.put(kv[0],kv[1]);
return map;
}).filter(e -> {
//根据自己选择筛选出需要的字段
if (((Map) e).containsKey("name") || ((Map) e).containsKey("age")) {
return true;
}
return false;
}).forEach(e->{
//循环map数组,获取想要的值
if(((Map) e).containsKey("name")){
System.out.println("name:"+((Map) e).getOrDefault("name",""));
}
if(((Map) e).containsKey("age")){
System.out.println("age:"+((Map) e).getOrDefault("age",""));
}
});