jquery 插件无法正确转换 JSON 字符串
我通过 comet 请求获取 JSON 字符串。该字符串如下:
"{"tab":2,"changedData":{"from":{"line":20,"ch":0},"to":{"line":20,"ch":0},"text":["a"]},"cmd":"copyChunk","timestamp":1329409543902,"person":{"comradeID":"4ef37369b4812","firstName":"","lastName":"","fullName":"ben team2","nickName":"ben t.","messageCount":0,"email":"[email protected]","lastPing":1329409537308,"sessionLeader":true,"cursorPosition":{"line":0,"ch":0}}}"
我通过 jquery json 插件运行它:
var r = $.evalJSON(jsonstring);
但它不会正确转换“changeData”对象。其余部分都有效,但changedData.from.line和changedData.to.line都导致NaN。
我还尝试将changedData.from.line作为字符串发送,然后使用
Number(changedData.from.line)
将其转换回数字,但它仍然返回NaN。我几乎确信 20 是一个数字,但我以前也曾错过。
提前致谢。
更新:
我很抱歉,开头和结尾的引号实际上不是字符串的一部分。复制/粘贴的陷阱。
这是上下文中的代码:
onMessage : function(frame)
{
//This function calls the handlers.
//this is fired every time we recieve a message from orbited
//this is what calls the handler functions
//body is a json string containing whatever data was sent via the send() function
delete r;
//convert the body to an object
var r = $.evalJSON(frame.body);
其中“frame.body”是上面发布的不带引号的字符串。
I'm getting a JSON string through a comet request. The string is as follows:
"{"tab":2,"changedData":{"from":{"line":20,"ch":0},"to":{"line":20,"ch":0},"text":["a"]},"cmd":"copyChunk","timestamp":1329409543902,"person":{"comradeID":"4ef37369b4812","firstName":"","lastName":"","fullName":"ben team2","nickName":"ben t.","messageCount":0,"email":"[email protected]","lastPing":1329409537308,"sessionLeader":true,"cursorPosition":{"line":0,"ch":0}}}"
I run it through the jquery json plugin:
var r = $.evalJSON(jsonstring);
But it will not convert the "changeData" object correctly. The rest of it all works, but changedData.from.line and changedData.to.line both result in NaN.
I've also tried sending changedData.from.line as a string and then using
Number(changedData.from.line)
to convert it back to a number, but it still returns NaN. I'm almost positive that 20 is a number, but I've been wrong before.
Thanks in advance.
Update:
I apologize, the quotes at the beginning and end aren't actually part of the string. The pitfalls of copy/paste.
Here is the code in context:
onMessage : function(frame)
{
//This function calls the handlers.
//this is fired every time we recieve a message from orbited
//this is what calls the handler functions
//body is a json string containing whatever data was sent via the send() function
delete r;
//convert the body to an object
var r = $.evalJSON(frame.body);
where "frame.body" is the string posted above without quotes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误在其他地方。使用
parseJSON
与您提供的字符串完美配合 http://jsfiddle。 net/mendesjuan/qG6Qv/1/您的问题可能是您将 parseJSON 的返回值分配给变量
r
然后您尝试执行以下操作数字(changedData.from.line)
。没有名为changedData
的变量Error is somewhere else. Using
parseJSON
works perfectly with the string you've given http://jsfiddle.net/mendesjuan/qG6Qv/1/It may be that your problem is that you're assigning the return of parseJSON to a variable
r
and then you're trying to doNumber(changedData.from.line)
. There is no variable calledchangedData
前导和尾随引号
"
无效。The leading and trailing quote marks
"
are invalid.