当前位置:主页 > 软件编程 > JAVA代码 >

json解析时遇到英文双引号报错的解决方法

时间:2021-06-05 08:57:22 | 栏目:JAVA代码 | 点击:

有时解析json时,会碰到里面带有英文的双引号,导致解析错误,可以将json进行转义,一下:

public static String htmlEscape(String input) {
if(isEmpty(input)){
  return input;
}
input = input.replaceAll("&", "&");
input = input.replaceAll("<", "&lt;");
input = input.replaceAll(">", "&gt;");
input = input.replaceAll(" ", "&nbsp;");
input = input.replaceAll("'", "&#39;");  //IE暂不支持单引号的实体名称,而支持单引号的实体编号,故单引号转义成实体编号,其它字符转义成实体名称
input = input.replaceAll("\"", "&quot;"); //双引号也需要转义,所以加一个斜线对其进行转义
input = input.replaceAll("\n", "<br/>"); //不能把\n的过滤放在前面,因为还要对<和>过滤,这样就会导致<br/>失效了
return input;
}

您可能感兴趣的文章:

相关文章