Javascript SyntaxError: JSON.parse() 的 Object.parse(本机)NodeJS 处的意外标记 u

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34355770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:12:14  来源:igfitidea点击:

SyntaxError: Unexpected token u at Object.parse (native) NodeJS for JSON.parse()

javascriptjsonnode.jsparsing

提问by rv.

I am in the learning stages of NodeJs and I was trying to get values of parameters from the json passed in the URL. I am using body parser because i saw many stack overflow answers using the same to parse through the data.

我正处于 NodeJs 的学习阶段,我试图从 URL 中传递的 json 中获取参数的值。我正在使用正文解析器,因为我看到许多堆栈溢出答案使用相同的方法来解析数据。

Below is the URL I am passing,

下面是我传递的网址,

http://localhost:1337/login?json={username:rv,password:password}

I am getting the error mentioned below,

我收到下面提到的错误,

SyntaxError: Unexpected token u
at Object.parse (native)
at C:\Users\summer\Desktop\nodejs\practise3.njs:14:17
at Layer.handle [as handle_request] (C:\Users\summer\Desktop\nodejs\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\summer\Desktop\nodejs\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (C:\Users\summer\Desktop\nodejs\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\summer\Desktop\nodejs\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\summer\Desktop\nodejs\node_modules\express\lib\router\index.js:277:22
at Function.process_params (C:\Users\summer\Desktop\nodejs\node_modules\express\lib\router\index.js:330:12)
at next (C:\Users\summer\Desktop\nodejs\node_modules\express\lib\router\index.js:271:10)
at jsonParser (C:\Users\summer\Desktop\nodejs\node_modules\body-parser\lib\types\json.js:100:40)

The code is mentioned below,

代码在下面提到,

var express = require('express');
var http = require('http');
var app = express();
var bodyparser = require('body-parser');

app.use(bodyparser.json());

app.get('/login',function(req,res,next){
    var content = '';
    var data = '';
    data = req.query.json;
    content = JSON.parse(data);        //I am getting the error here
    res.writeHead(200,{"Content-type":"text/plain"});
    res.write(data);
    res.end();
});

http.createServer(app).listen(1337);
console.log("Server Started successfully at Port 1337");

Note: After reading this question, if you know other alternatives for collecting values from json data, please do tell.

注意:阅读此问题后,如果您知道从 json 数据中收集值的其他替代方法,请务必告知。

回答by Subburaj

Instead of this:

取而代之的是:

data = req.query.json;
content = JSON.parse(data);        //I am getting the error here

Try for this:

试试这个:

data = req.query.json;
var stringify = JSON.stringify(data)
content = JSON.parse(stringify);       

回答by BenC

JSON.parseis choking because unlike Javascript, JSON requires all key names to be in quotes [0], so your JSON should be

JSON.parse令人窒息,因为与 Javascript 不同,JSON 要求所有键名都用引号 [0] 括起来,因此您的 JSON 应该是

{"username":rv,"password":password}

The error "Unexpected token u..." is occurring when the JSON parser encounters the "u" at the beginning of "username", when it expected a quotation mark.

当 JSON 解析器在“用户名”开头遇到“u”时,会发生错误“意外的令牌 u...”,而它需要引号。

[0] There is a very readable summary of the JSON spec at http://json.org.

[0] http://json.org 上有一个非常易读的 JSON 规范摘要。

回答by Hari Das

This happens because the key name must be enclosed within double quotes. Here is a workaround to overcome this issue:

发生这种情况是因为键名必须用双引号括起来。以下是解决此问题的解决方法:

var jsonObj = eval('(' + YOUR_JSON_STRING + ')');