说明:由于原贴过去太久无法回复,故在此记录,以防后续有用。原贴地址:https://bbs.csdn.net/topics/390578406?page=1#new_post (12楼回答)
原文代码中存在一些小问题,以下代码针对冒号(:)和引号(")之间存在空格符的问题进行优化:
private static String jsonString(String s) {char[] temp = s.toCharArray();int n = temp.length;for (int i = 0; i < n; i++) {if (temp[i] == ':') {int quentIndex = i + 1;while (StringUtils.isBlank(String.valueOf(temp[quentIndex]))) {quentIndex++;}if (temp[quentIndex] == '"') {for (int j = quentIndex + 1; j < n; j++) {if (temp[j] == '"') {if (temp[j + 1] != ',' && temp[j + 1] != '}') {temp[j] = '”';} else if (temp[j + 1] == ',' || temp[j + 1] == '}') {break;}}}}}}return new String(temp);
}