public static void main(String[] args) {
    String info = "[{\"SYNC_930000003690\":{\"finalSYSTEMNAME\":\"PXY-CPCP配置中心\",\"P_REQUEST_ID\":\"585471439551123454\",\"finalSYSTEMTYPE\":\"BSS-PXY\",\"finalDeveloperName\":\"\",\"finaltaxtCost\":\"\",\"finalworkRemark\":\"\",\"formId\":930000003690,\"REQUEST_ID\":\"A585545039142506496\",\"finalSuplIptDays\":\"22\"}},{\"SYNC_930000003690\":{\"finalSYSTEMNAME\":\"PXY-CPCP配置中心\",\"P_REQUEST_ID\":\"585471439551123455\",\"finalSYSTEMTYPE\":\"BSS-PXY\",\"finalDeveloperName\":\"\",\"finaltaxtCost\":\"\",\"finalworkRemark\":\"\",\"formId\":930000003694,\"REQUEST_ID\":\"B585545039142506496\",\"finalSuplIptDays\":\"44\"}},{\"SYNC_930000003690\":{\"finalSYSTEMNAME\":\"PXY-CPCP配置中心\",\"P_REQUEST_ID\":\"585471439551123456\",\"finalSYSTEMTYPE\":\"BSS-PXY\",\"finalDeveloperName\":\"\",\"finaltaxtCost\":\"\",\"formId\":930000003692,\"finalworkRemark\":\"\",\"REQUEST_ID\":\"C585545039142506496\",\"finalSuplIptDays\":\"55\"}}]";
    String keyField = "P_REQUEST_ID";
    System.out.println("=====    match    "+ keyField + " =========");
    //  \w : 数字,字母 ,下划线 0-9 a-z A-Z _
    String patternStr = keyField+"\":\"([0-9a-zA-Z_]+)?\"";
    matchJsonValue(info,patternStr);
 
    keyField = "formId";
    System.out.println("=====    match    "+ keyField + " =========");
    patternStr = keyField+"\":(\\d+)?";
    matchJsonValue(info,patternStr);
}
 
/**
 * 正则表达式 查找匹配json字符串对应属性的值
 * @param sourcStr 字符串
 * @param patternStr 正则表达式
 * @return 字符集
 */
public static List<String> matchJsonValue(String sourcStr, String patternStr) {
    List<String> aimList = new ArrayList<>();
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(sourcStr);
    while (matcher.find()) {
        System.out.println("group all: " + matcher.group(0));
        System.out.println("aim index: " + matcher.group(NumberUtils.INTEGER_ONE));
        aimList.add(matcher.group(NumberUtils.INTEGER_ONE));
    }
    return aimList;
}
public static String getValueFromJsonStr(String jsonStr, String fieldName) {
        if (StringUtils.isEmpty(fieldName)) {
            return fieldName;
        }
        String regex = "(?<=(\"" + fieldName + "\":\")).*?(?=(\"))";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(jsonStr);
        while (matcher.find()) {
            if (StringUtils.isNotEmpty(matcher.group().trim())) {
                return matcher.group().trim();
            }
        }
        return "";
    }

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐