1、当用JSON.parse()方法,解析从后台传来的json字符串,而且字符串中含有单引号(如I'am ....),页面将出现如下异常
2、页面引起报错部分
bookBlockVoListJSONString = BaseStringUtils.getJsonForJS(JSONArray.fromObject(bookBlockVoList).toString());
--页面
//如果为做作业,则下面两个变量有值
var _homeworkRecordId = '${homeworkRecordId}';
var _homeworkType = '${homeworkType}';
var _blockList = null;
if( _homeworkRecordId != null && _homeworkRecordId != "" ){
_blockList = JSON.parse('${bookBlockVoListJSONString}');
}
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; } }原文链接:https://www.f2er.com/json/290050.html