后端向前端发送JSON数据报错

(1)json文件中如果出现单个的反斜杠'\',浏览器会报jsonParse error,这是因为反斜杠在json中必须使用''\\'才能正确。

(2)使用string.replaceAll("\\","\")出错。

java.util.regex.PatternSyntaxException: Unexpected internal error near index 1 \^

  在regex中"\\"表示一个"\",在java中一个"\"也要用"\\"表示。这样,前一个"\\"代表regex中的"\",后一个"\\"代表java中的"\"。所以要想使用replaceAll方法将字符串中的反斜杠("\")替换成空字符串(""),则需要这样写:string.replaceAll("\\\\","");

因此替换\的正确写法是

string.replaceAll("\\\\","\");

 

posted on 2017-03-01 20:15  codeDog123  阅读(211)  评论(0编辑  收藏  举报

导航