[英]Strange JSON.parse() error in node.js
I'm retrieving some stringifyed JSON via TCP in node.js an want to parse it. 我正在node.js中通过TCP检索一些stringifyed JSON,想要解析它。 So my approach is similiar to this.
所以我的方法与此类似。 I shortend and simplified it, so you don't have to know the surrounding logic.
我缩短并简化了它,所以你不必知道周围的逻辑。
socket.on("data", function(data) {
console.log(data.toString()); // Shows the original stringifyed version
console.log(JSON.parse(data.toString())); // Doesn't work
});
The complete (beautified) JSON is this. 完整的(美化的)JSON就是这样。 As you can see, there are no errors.
如您所见,没有错误。
{
"result": "success",
"source": "chat",
"success": {
"message": "test",
"time": 1331770513,
"player": "silvinci"
}
}
But JSON.parse(data.toString())
always throws me this dumb error: 但是
JSON.parse(data.toString())
总是抛出这个愚蠢的错误:
{"result":"success","source":"console","success":{"time":1331762264,"line":"20
^
SyntaxError: Unexpected token {
at Object.parse (native)
at Socket.<anonymous> (/home/node/api.js:152:35) // irrelevant from here on
at Socket.emit (events.js:67:17)
at TCP.onread (net.js:347:14)
So I thougt: "What could be wrong with the JSON-String. Let's try it directly. Should not work." 所以我想:“JSON-String可能出现什么问题。让我们直接尝试。不应该工作。” Surprise, Surprise!
惊喜,惊喜! It worked.
有效。 Why does it work when I directly enter the String?
当我直接输入字符串时为什么它可以工作?
Thanks to @ Felix Kling I found my bug. 感谢@ Felix Kling我发现了我的错误。 It's very important to filter unescaped characters, especially outside the stringified JSON.
过滤未转义的字符非常重要,尤其是在字符串化的JSON之外。 I didn't and overlooked an invisible linebreak right after the stringified JSON.
我没有并且在字符串化的JSON之后忽略了一个看不见的换行符。
This is the fix: 这是修复:
socket.on("data", function(data) {
console.log(data.toString()); // Shows the original stringified version
console.log(JSON.parse(data.toString().slice(0, -4))); // Trim the sequence "\r\n" off the end of the string
});
Please note, that this only works for me, as I have a very specialized case. 请注意, 这只适用于我,因为我有一个非常专业的案例。 The server is always responding in lines of JSON, that are terminated with an
\\r\\n
- not the whitespace characters, but literally backslash r and backslash n. 服务器始终以JSON行响应,以
\\r\\n
结尾 - 而不是空白字符,但字面反斜杠r和反斜杠n。
Your code may (or probably) fail due to other errors. 您的代码可能(或可能)因其他错误而失败。 But checking the server's response is a good starting point when you get parsing errors.
但是,当您解析错误时,检查服务器的响应是一个很好的起点。
As @ Zack correctly pointed out, this is a more general fix for removing unintended whitespace: 正如@Zack正确指出的那样,这是一个更通用的修复方法,用于删除意外的空格:
JSON.parse(data.toString().trim());
I had a similar issue. 我有一个类似的问题。 For a more general solution, this will work too.
对于更通用的解决方案,这也将起作用。 It takes off all whitespace before and after the string so you don't have to do the specific substring length.
它取消了字符串之前和之后的所有空格,因此您不必执行特定的子字符串长度。
JSON.parse(data.trim());
Instead of blindly removing last 4 chars, I suggest to remove the offending chars only: 我建议只删除有问题的字符,而不是盲目删除最后4个字符:
socket.on("data", function(data) {
console.log(data.toString()); // Shows the original stringified version
console.log(JSON.parse(data.toString().replace('\r','').replace('\n',''))); // Trim the sequence "\r\n" off the end of the string
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.