JSON.parse 抛出的意外令牌 e
Unexpected token e thrown by JSON.parse
我正在尝试使用 FileReader 从文件中读取 JSON 对象。
此 json 文件包含以下内容:
{"markers": [
{
"point":new GLatLng(40.266044,-74.718479),
"homeTeam":"Lawrence Library",
"awayTeam":"LUGip",
"markerImage":"images/red.png",
"information": "Linux users group meets second Wednesday of each month.",
"fixture":"Wednesday 7pm",
"capacity":"",
"previousScore":""
},
{
"point":new GLatLng(40.211600,-74.695702),
"homeTeam":"Hamilton Library",
"awayTeam":"LUGip HW SIG",
"markerImage":"images/white.png",
"information": "Linux users can meet the first Tuesday of the month to work out harward and configuration issues.",
"fixture":"Tuesday 7pm",
"capacity":"",
"tv":""
},
{
"point":new GLatLng(40.294535,-74.682012),
"homeTeam":"Applebees",
"awayTeam":"After LUPip Mtg Spot",
"markerImage":"images/newcastle.png",
"information": "Some of us go there after the main LUGip meeting, drink brews, and talk.",
"fixture":"Wednesday whenever",
"capacity":"2 to 4 pints",
"tv":""
},
] }
这是我写的代码:
var jsonClean = function(jsonText) {
return jsonText.replace(/\n/g, "").replace(/\r/g, "").replace(/\t/g, "");
};
if (window.File && window.FileReader && window.FileList && window.Blob) {
console.log('Uploading...');
var uploader = ($('input#fileUpload'))[0];
var fileList = uploader.files;
console.log(fileList.length);
if(fileList.length > 0){
var reader = new FileReader();
reader.addEventListener('load',function(loadEvent){
if(reader.readyState == FileReader.DONE){
var jsonObject = jsonClean(reader.result);
console.log(jsonObject);
$('#jsonView').empty();
$('#jsonView').JSONView(JSON.parse(jsonObject));
}
});
reader.readAsText(fileList[0]);
}
}
else {
alert('The File APIs are not fully supported in this browser.');
}
jsonconsole.log() 打印的对象:
{"markers": [{"point":new GLatLng(40.266044,-74.718479), "homeTeam":"Lawrence Library","awayTeam":"LUGip","markerImage":"images/red.png","information": "Linux users group meets second Wednesday of each month.","fixture":"Wednesday 7pm","capacity":"","previousScore":""},{"point":new GLatLng(40.211600,-74.695702),"homeTeam":"Hamilton Library","awayTeam":"LUGip HW SIG","markerImage":"images/white.png","information": "Linux users can meet the first Tuesday of the month to work out harward and configuration issues.","fixture":"Tuesday 7pm","capacity":"","tv":""},{"point":new GLatLng(40.294535,-74.682012),"homeTeam":"Applebees","awayTeam":"After LUPip Mtg Spot","markerImage":"images/newcastle.png","information": "Some of us go there after the main LUGip meeting, drink brews, and talk.","fixture":"Wednesday whenever","capacity":"2 to 4 pints","tv":""},] }
'Uncaught SyntaxError:Unexpected token e' 在调用 JSONView() 时抛出。
有人可以指出代码有什么问题吗?谢谢。
这对初学者来说是无效的 JSON。这些位:new GLatLng(40.266044,-74.718479)
...不能是 JavaScript class 构造函数;将它们更改为 the JSON spec 中定义的类型之一。
我正在尝试使用 FileReader 从文件中读取 JSON 对象。 此 json 文件包含以下内容:
{"markers": [
{
"point":new GLatLng(40.266044,-74.718479),
"homeTeam":"Lawrence Library",
"awayTeam":"LUGip",
"markerImage":"images/red.png",
"information": "Linux users group meets second Wednesday of each month.",
"fixture":"Wednesday 7pm",
"capacity":"",
"previousScore":""
},
{
"point":new GLatLng(40.211600,-74.695702),
"homeTeam":"Hamilton Library",
"awayTeam":"LUGip HW SIG",
"markerImage":"images/white.png",
"information": "Linux users can meet the first Tuesday of the month to work out harward and configuration issues.",
"fixture":"Tuesday 7pm",
"capacity":"",
"tv":""
},
{
"point":new GLatLng(40.294535,-74.682012),
"homeTeam":"Applebees",
"awayTeam":"After LUPip Mtg Spot",
"markerImage":"images/newcastle.png",
"information": "Some of us go there after the main LUGip meeting, drink brews, and talk.",
"fixture":"Wednesday whenever",
"capacity":"2 to 4 pints",
"tv":""
},
] }
这是我写的代码:
var jsonClean = function(jsonText) {
return jsonText.replace(/\n/g, "").replace(/\r/g, "").replace(/\t/g, "");
};
if (window.File && window.FileReader && window.FileList && window.Blob) {
console.log('Uploading...');
var uploader = ($('input#fileUpload'))[0];
var fileList = uploader.files;
console.log(fileList.length);
if(fileList.length > 0){
var reader = new FileReader();
reader.addEventListener('load',function(loadEvent){
if(reader.readyState == FileReader.DONE){
var jsonObject = jsonClean(reader.result);
console.log(jsonObject);
$('#jsonView').empty();
$('#jsonView').JSONView(JSON.parse(jsonObject));
}
});
reader.readAsText(fileList[0]);
}
}
else {
alert('The File APIs are not fully supported in this browser.');
}
jsonconsole.log() 打印的对象:
{"markers": [{"point":new GLatLng(40.266044,-74.718479), "homeTeam":"Lawrence Library","awayTeam":"LUGip","markerImage":"images/red.png","information": "Linux users group meets second Wednesday of each month.","fixture":"Wednesday 7pm","capacity":"","previousScore":""},{"point":new GLatLng(40.211600,-74.695702),"homeTeam":"Hamilton Library","awayTeam":"LUGip HW SIG","markerImage":"images/white.png","information": "Linux users can meet the first Tuesday of the month to work out harward and configuration issues.","fixture":"Tuesday 7pm","capacity":"","tv":""},{"point":new GLatLng(40.294535,-74.682012),"homeTeam":"Applebees","awayTeam":"After LUPip Mtg Spot","markerImage":"images/newcastle.png","information": "Some of us go there after the main LUGip meeting, drink brews, and talk.","fixture":"Wednesday whenever","capacity":"2 to 4 pints","tv":""},] }
'Uncaught SyntaxError:Unexpected token e' 在调用 JSONView() 时抛出。
有人可以指出代码有什么问题吗?谢谢。
这对初学者来说是无效的 JSON。这些位:new GLatLng(40.266044,-74.718479)
...不能是 JavaScript class 构造函数;将它们更改为 the JSON spec 中定义的类型之一。