js, ie8 下 特殊字符导致转成json对象报错

在对含特殊字符JSON数组字符串转换成json对象时报语法错误,无论用eval还是JSON.parse都没用,
谷歌、IE9都没问题,通过debugger发现是因为字符串中含特殊字符导致前台报语法错误

theString = theString.Replace(">", ">");
theString = theString.Replace("<", "<");
theString = theString.Replace(" ", " ");
theString = theString.Replace("\"", """);
theString = theString.Replace("\'", "'");
theString = theString.Replace("\\", "\\\\");//对斜线的转义
theString = theString.Replace("\n", "\\n");
theString = theString.Replace("\r", "\\r");
除了以上方法还有其它方法?因为转成对象之后 还得转回去

跪求大神指导

我之前也遇到这个情况不知道适不适合你
var str='json字符串';//我之前这里是后台返回的
var json=eval("("+str+")");//这里没有特殊字符无错误,有的话会报错
后来我把var str='json字符串'改为var str=json字符串;//即不要'',就可以了
不知道可不可以帮到你
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-03-09
推荐使用json2.js追问

。。在Html直接引用这个文件就行了?

追答

对的

本回答被提问者采纳