1、当用JSON.parse()方法,解析从后台传来的json字符串,而且字符串中含有单引号(如I'am ....),页面将出现如下异常
2、页面引起报错部分
3、解决办法:在后台将特殊字符转义
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;}}