我正在尝试使用 javascript 解析一个大型 json 文件(240'000 个字符)。我正在使用 ajax 从 servlet 检索 json 数据。我使用的代码适用于较小的样本,但是当 xmlHttp.responseText 包含大量 json 数据时,它就会抛出这个问题。
Uncaught SyntaxError: Unexpected token ILLEGAL
mycallbackFunction
xmlHttp.onreadystatechange
它说意外的令牌在包含的行上
var data = eval('(' + xmlHttp.responseText + ')');
这是代码的要点:
getJsonData(mycallbackFunction, parameters);
function getJsonData(callbackFunction, parameters){
var xmlHttp = new XMLHttpRequest();
xmlHttp.open('GET', servlet_url + parameters, true);
xmlHttp.onreadystatechange = function(){
callbackFunction(xmlHttp);
}
xmlHttp.setRequestHeader('Content-Type', 'application/json');
xmlHttp.send(null);
}
function mycallbackFunction(xmlHttp){
var data = eval('(' + xmlHttp.responseText + ')');
}
使用 eval() 的方法是从 xmlHttp.onreadystatechange 调用的,如果这有什么不同的话。
我也尝试使用 json2.js 并获得相同的结果,它适用于较小的 json 样本,但是当我尝试使用我的 240k 字符文件时,它说:
Uncaught #<Object>
提前致谢。