JSON+Node.js - 意外的令牌 o
Posted
技术标签:
【中文标题】JSON+Node.js - 意外的令牌 o【英文标题】:JSON+Node.js - Unexpected token o 【发布时间】:2012-09-21 04:59:06 【问题描述】:我刚开始在大学课程中使用 node.js 和 json 对象。我们本周的一项任务是创建一些 json 对象并将对象的一部分提取到一个 html 页面中。我以为我很好地掌握了如何做到这一点,但是当我尝试启动 node.js 时遇到了错误。如果我删除了 colleges 对象和 parse 语句,则节点运行良好。
这是我在运行“node index.js”时遇到的错误:
undefined:1
[object Object],[object Object],[object Object],[object Object],[object Object
^
SyntaxError: Unexpected token o
at Object.parse (native)
at Object.<anonymous> (/home/ubuntu/node_stuff/node_json/requestHandlers.js:13:20)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/home/ubuntu/node_stuff/node_json/index.js:3:23)
at Module._compile (module.js:449:26)
这是我正在使用的代码。
var querystring = require("querystring"),
fs = require("fs"),
formidable = require("formidable");
var colleges = [
"name":"A-B Tech","street":"340 Victoria Road","city":"Asheville","state":"NC","zip":"28801","phone":"828-254-1921",
"name":"UNC Asheville","street":"1 University Heights","city":"Asheville","state":"NC","zip":"28804","phone":"828-251-6600",
"name":"UNC Charlotte","street":"9201 University City Blvd","city":"Charlotte","state":"NC","zip":"28223","phone":"704-687-8622",
"name":"Western Carolina","street":"North Carolina 107","city":"Cullowhee","state":"NC","zip":"28723","phone":"877-928-4968",
"name":"NC State","street":"2200 Hillsborough","city":"Raleigh","state":"NC","zip":"27695","phone":"919-515-2011"
];
var college = JSON.parse(colleges);
function abtech(response)
console.log("Request handler 'abtech' was called.");
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<ul>'+
'<li>' + college[0].name + '</li>'+
'<li>' + college[0].street + '</li>'+
'<li>' + college[0].city + ' ' + college[0].state + ' ' + college[0].zip + '</li>'+
'<li>' + college[0].phone + '</li>'+
'</ul>'+
'</body>'+
'</html>';
response.writeHead(200, "Content-Type": "text/html");
response.write(body);
response.end();
function unca(response)
console.log("Request handler 'abtech' was called.");
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<ul>'+
'<li></li>'+
'<li></li>'+
'<li></li>'+
'<li></li>'+
'</ul>'+
'</body>'+
'</html>';
response.writeHead(200, "Content-Type": "text/html");
response.write(body);
response.end();
function home(response)
console.log("Request handler 'home' was called.");
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<h1>Welcome to College</h2>'+
'<p>Where would you like to visit?</p>'+
'<ul>'+
'<li><a href="/colleges">Colleges</a></li>'+
'<li><a href="/hours">Hours of Operation</a></li>'+
'<li><a href="/start">Upload a Photo</a></li>'+
'<li><a href="/show">View Gallery</a></li>'+
'</ul>'+
'</body>'+
'</html>';
response.writeHead(200, "Content-Type": "text/html");
response.write(body);
response.end();
function colleges(response)
console.log("Request handler 'colleges' was called.");
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<h1>Colleges</h2>'+
'<ul>'+
'<li><a href="/abtech">A-B Tech</a></li>'+
'<li><a href="/unca">UNC Asheville</a></li>'+
'<li><a href="/uncc">UNC Charlotte</a></li>'+
'<li><a href="/wcu">Western Carolina</a></li>'+
'<li><a href="/ncsu">NC State</a></li>'+
'</ul>'+
'</body>'+
'</html>';
response.writeHead(200, "Content-Type": "text/html");
response.write(body);
response.end();
function hours(response)
console.log("Request handler 'gallery' was called.");
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<h1>Hours of Operation</h2>'+
'<table>'+
'<tr><td>Monday - Thursday</td><td>9 a.m. - 7 p.m.</td></tr>'+
'<tr><td>Friday</td><td>9 a.m. - 5 p.m.</td></tr>'+
'<tr><td>Saturday</td><td>9 a.m. - 12 p.m.</td></tr>'+
'</table>'+
'</body>'+
'</html>';
response.writeHead(200, "Content-Type": "text/html");
response.write(body);
response.end();
function start(response)
console.log("Request handler 'start' was called.");
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<h1>Upload a file</h2>'+
'<p>It will be shown on the /show url after</p>'+
'<form action="/upload" enctype="multipart/form-data" '+
'method="post">'+
'<input type="file" name="upload" multiple="multiple">'+
'<input type="submit" value="Upload file" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200, "Content-Type": "text/html");
response.write(body);
response.end();
function upload(response, request)
console.log("Request handler 'upload' was called.");
var form = new formidable.IncomingForm();
console.log("about to parse");
form.parse(request, function(error, fields, files)
console.log("parsing done");
/* Possible error on Windows systems:
tried to rename to an already existing file */
fs.rename(files.upload.path, "/home/ubuntu/node_stuff/node_assignment/test.jpg", function(err)
if (err)
fs.unlink("/home/ubuntu/node_stuff/node_assignment/test.jpg")
fs.rename(files.upload.path, "/home/ubuntu/node_stuff/node_assignment/test.jpg");
);
response.writeHead(200, "Content-Type": "text/html");
response.write("received image:<br/>");
response.write("<img src='/show' />");
response.end();
);
function show(response)
console.log("Request handler 'show' was called.");
fs.readFile("/home/ubuntu/node_stuff/node_assignment/test.jpg", "binary", function(error, file)
if(error)
response.writeHead(500, "Content-Type": "text/plain");
response.write(error + "\n");
response.end();
else
response.writeHead(200, "Content-Type": "image/jpg");
response.write(file, "binary");
response.end();
);
exports.start = start;
exports.upload = upload;
exports.show = show;
exports.home = home;
exports.colleges = colleges;
exports.hours = hours;
exports.abtech = abtech;
非常感谢任何关于我做错了什么的提示。我的教练在周末很难找到,所以我真的没有其他地方可以转。谢谢。
【问题讨论】:
使用 typeof(yourVar); 总是一个好主意看看你正在使用什么。许多框架(Express、Restify 等)会为您解析 JSON——尤其是当它通过 HTTP 请求进入时。 【参考方案1】:解决方法是使用JSON.stringify
如下
var target=JSON.parse(JSON.stringify(source));
【讨论】:
你为什么要回答一个已经有有效答案的 4 年前的问题?就像...为什么要解析一个字符串化的对象?就target = source
...
我已经尝试了给定的答案,但它对我不起作用。 source 是一个对象,所以要调用 parse 我们需要把它变成一个字符串(忘记 target=source 只是一个例子)
您正在解析对象的字符串化版本...就像将数字转换为字符串只是为了将其转换回数字之后...
这样做可以避免我在位置 1 错误的 JSON 中出现意外的标记 o【参考方案2】:
您的 colleges
变量已经是一个有效的 javascript 对象。你不必在上面使用JSON.parse
。
JSON.parse
需要 String
作为第一个参数,但您提供了 Object
。因此,它被强制转换为 String
,看起来就像您在错误消息中看到的那样。
对于您的其余代码,您可能需要查看 Express 或 Zappa 以便能够编写更紧凑的代码;)
【讨论】:
谢谢塔拉巴斯。没有意识到我不需要 parse 语句。当老师在课堂上演示时,他使用了它。现在可以正常工作了。我们很快就会讨论 Express 和 MongoDB。 拯救了这一天。谢谢。var target=JSON.parse(JSON.stringify(source));
以上是关于JSON+Node.js - 意外的令牌 o的主要内容,如果未能解决你的问题,请参考以下文章
解析错误:Firebase 云函数中出现意外的令牌 selectWinner - Node.js V10
加载socket.io.js时Node.js“未捕获的语法错误:意外的令牌<”