Java 处理json字符串value中多余的双引号

一、错误场景

  json字符串的value值中有多余的双引号。

  错误的json字符串

 二、处理方案

自己写个方法将value值中多余的双引号替换为 中文双引号:

    // 处理json字符串中value多余的双引号, 将多余的双引号替换为中文双引号
    private static String toJsonString(String s) {
        char[] tempArr = s.toCharArray();
        int tempLength = tempArr.length;
        for (int i = 0; i < tempLength; i++) {
            if (tempArr[i] == ':' && tempArr[i + 1] == '"') {
                for (int j = i + 2; j < tempLength; j++) {
                    if (tempArr[j] == '"') {
                        if (tempArr[j + 1] != ',' && tempArr[j + 1] != '}') {
                            tempArr[j] = '”'; // 将value中的 双引号替换为中文双引号
                        } else if (tempArr[j + 1] == ',' || tempArr[j + 1] == '}') {
                            break;
                        }
                    }
                }
            }
        }
        return new String(tempArr);
    }

}
原文地址:https://www.cnblogs.com/nemowang1996/p/12459389.html