正则表达式 获取JSON数据中的value值

本文介绍如何通过正则表达式从JSON字符串中有效地获取value值,适用于处理和解析JSON数据的场景。
public void getPhoneStr(String jsonString, Set<String> set) {
    String regex = "\"phone\":\"(.*?)\\\"";//使用非贪婪模式
    Matcher matcher = Pattern.compile(regex).matcher(jsonString);
    while (matcher.find()) {
        set.add(matcher.group(1));
    }
}
public static void main(String[] args) {
    String jsonString = "{\"page_size\":\"12321321\",\"page_size\":\"123\",\"page_index\":0,\"page_count\":0,\"has_next\":false,\"datas\":null,\"code\":0,\"issuccess\":true,\"msg\":null}";
    String regex = "\"page_size\":\"(.*?)\\\"";//使用非贪婪模式
    Matcher matcher = Pattern.compile(regex).matcher(jsonString);
    while (matcher.find()) {
        String ret = matcher.group(1);
        System.out.println("key:" + matcher.group(1));
    }

    String str = "{\"keyword\":\"xin \"hua\",\"type\":\"news\",\"countryName\":\"not中国\"," +
            "\"pageNo\":1,\"pageSize\":10,\"beginDate\":\"\"\"2019-11-23 23:59:59\"\"\"," +
            "\"endDate\":\"2019-11-23 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 matcher1 = pattern.matcher(str);
    while (matcher1.find()){
        if(matcher1.group(1)!=null) {
            //System.out.println("key:" + matcher1.group(1));
            System.out.println("value:" + matcher1.group(2));
        }else{
            // System.out.println("key:" + matcher1.group(3));
            System.out.println("value:" + matcher1.group(4));
        }
    }
}

返回结果

key
12321321
123
value
xin “hua
news
not中国
1
10
”“2019-11-23 23:59:59"”
2019-11-23 23:59:59
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值