java正则匹配json

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014589856/article/details/80280877

有些时候如果json格式数据里如果value带有双引号,则这样的数据无法直接用json工具解析出来,需要手动去处理,下边的正则表达式匹配出带有双引号的value。

String str="{\"keyword\":\"xin \"hua\",\"type\":\"news\",\"countryName\":\"not中国\"," +
        "\"pageNo\":1,\"pageSize\":10,\"beginDate\":\"\"\"2018-05-01 00:00:00\"\"\"," +
        "\"endDate\":\"2018-05-09 23:59:59\"}";
Pattern pattern = Pattern.compile("\"([a-zA-z0-9]{0,})\":\"{1}([a-zA-z0-9\\-\\s\\:\\u4e00-\\u9fa5\"]{0,})\"{1}[\\,\\}]{1}|" +
        "\"([a-zA-z0-9]{0,})\":([a-zA-z0-9\\-\\s\\:\\u4e00-\\u9fa5\"]{0,})[\\,\\}]{1}");
Matcher matcher = pattern.matcher(str);
while (matcher.find()){
    if(matcher.group(1)!=null) {
        System.out.println("key:" + matcher.group(1));
        System.out.println("value:" + matcher.group(2));
    }else{
        System.out.println("key:" + matcher.group(3));
        System.out.println("value:" + matcher.group(4));
    }
}



猜你喜欢

转载自blog.csdn.net/u014589856/article/details/80280877