JSON.parse()解析单引号错误的问题
1、当用JSON.parse()方法,解析从后台传来的json字符串,而且字符串中含有单引号(如I'am ....),页面将出现如下异常
2、页面引起报错部分
3、解决办法:在后台将特殊字符转义 bookBlockVoListJSONString = BaseStringUtils.getJsonForJS(JSONArray.fromObject(bookBlockVoList).toString());
--页面 //如果为做作业,则下面两个变量有值
4、转义代码 public class BaseStringUtils { /** * 转换特殊字符,将json串转换为JS能直接识别的json * @param oldJson * @return */ public static String getJsonForJS(String oldJson) { String newJson = oldJson; newJson = newJson.replaceAll("\","\\"); newJson = newJson.replaceAll("'","\'"); newJson = newJson.replaceAll(""","\""); return newJson; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |