String response = "{\"phone\":[{\"firstName\":\"sandeepan\",\"lastName\":\"\"}]}";
System.out.println("response "+response);
Pattern ps = Pattern.compile("/\\{\"\\.\\*\"firstName\":\"([a-z]*)\"\\.\\*/");
Matcher m = ps.matcher(response);
while(m.find()) {
System.out.println("matched "+m.group());
}
System.out.println("After matching block");
输出:
response {"phone":[{"firstName":"sandeepan","lastName":""}]}
After matching block
不过,正则表达式正在发挥作用-
https://regex101.com/r/L2vZ9J/1/
为什么我们使用正则表达式而不是json解析:
-
对于给定的电话号码,响应中可能有多组数据,我们可以获取第一组数据。
-
第一个属性中有一个值(“phone”是一个值,而不是一个键,所以需要将其建模为一个类,这样才能正确地执行。)
这只是一项一次性任务,所以我们需要快速完成,因为实施时间不多。