Java中[xxx:xxx,aaa:aaa]格式字符串解析

Java中[xxx:xxx,aaa:aaa]格式字符串解析

String str = "[name:张三,age:18,phone:15888887777,email:[email protected]]";
//转换成json数组 package net.sf.json
JSONArray input = JSONArray.fromObject(str);
input.stream().map(e -> {
			 //将json数组转换成map并返回
              Map 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",""));
              }
          });

你可能感兴趣的:(java,java,json)