javascript 使用 WebSocket 解析收到的 JSON 导致错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7116035/
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
Parse JSON received with WebSocket results in error
提问by stepan
I have written a very simple test application that creates a websocket and parses the data it receives (server sends valid JSON data). The problem is that the first JSON object is parsed successfully, but all the subsequent objects are parsed with errors.
我编写了一个非常简单的测试应用程序,它创建一个 websocket 并解析它接收到的数据(服务器发送有效的 JSON 数据)。问题是第一个JSON对象解析成功,但是后面的所有对象都解析出错。
Here's all the code:
这是所有的代码:
$("#connect").click(function ()
{
socket = new WebSocket("my server address");
socket.onopen = function ()
{
$("#log").append("Connection opened.<br/>");
socket.send(/* login information goes here */));
};
socket.onerror = function (e)
{
$("#log").append("Error: " + e.data + "<br/>");
};
socket.onclose = function ()
{
$("#log").append("Connection closed.<br/>");
};
socket.onmessage = function (e)
{
$("#log").append(index.toString() + ": " + e.data + "<br/><br/>");
console.log("Parsing " + index);
index++;
var obj = JSON.parse(e.data);
console.log("Parsed:");
console.log(obj);
};
});
What I'm getting is: The first time "socket.onmessage" called - JSON is parsed and JS console displays an object. When second one arrives it outputs it to my "log", but JSON.parse fails with error "Uncaught SyntaxError: Unexpected token ILLEGAL". What is puzzling me is that the string that is received is a valid JSON object - I have tested it through several JSON validators. I have even copy-pasted it from my "log" put it in a separate file and parsed it with $.getJSON - and it worked fine, no errors.
我得到的是:第一次调用“socket.onmessage” - 解析 JSON 并且 JS 控制台显示一个对象。当第二个到达时,它将它输出到我的“日志”,但 JSON.parse 失败并显示错误“Uncaught SyntaxError: Unexpected token ILLEGAL”。让我感到困惑的是,收到的字符串是一个有效的 JSON 对象——我已经通过几个 JSON 验证器对其进行了测试。我什至从我的“日志”中复制粘贴它,将它放在一个单独的文件中,并用 $.getJSON 解析它 - 它工作正常,没有错误。
Browser: Chrome 13.0.782.112
浏览器:Chrome 13.0.782.112
Any ideas would be helpful.
任何想法都会有所帮助。
Thank you.
谢谢你。
采纳答案by HBP
ES5 spec http://ecma262-5.com/ELS5_HTML.htm#Section_15.12.1defines JSON whitespace as tab, cr, lf or sp. The Crockford skip-space uses the following code :
ES5 规范http://ecma262-5.com/ELS5_HTML.htm#Section_15.12.1将 JSON 空格定义为 tab、cr、lf 或 sp。Crockford 跳过空间使用以下代码:
white = function () {
// Skip whitespace.
while (ch && ch <= ' ') {
next();
}
},
So if you have any spurious null characters or form-feeds etc in your response then the ES5 JSON parse will throw an error while the Crockford version will not.
因此,如果您的响应中有任何虚假的空字符或表单馈送等,那么 ES5 JSON 解析将引发错误,而 Crockford 版本则不会。
回答by Michael
You should do:
你应该做:
$.parseJSON( json );
see this for more info: http://api.jquery.com/jQuery.parseJSON/
有关更多信息,请参阅:http: //api.jquery.com/jQuery.parseJSON/