例如Json數(shù)據(jù)中有雙引號,解析時會報Json格式錯誤或者轉(zhuǎn)義有問題
{
"content": "重要任務(wù)提醒",
"matterName": ""德勤57"裝砂滯留船期損失案"
}
可以在解析前現(xiàn)將Json格式化一遍,這里面主要是雙引號匹配的查找
public static String formatErrorJson(String s) {
char[] temp = s.toCharArray();
int n = temp.length;
for (int i = 0; i < n; i++) {
if (temp[i] == ':' && temp[i + 1] == '"') {
for (int j = i + 2; 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);
}